I don’t mean an alias or function. I actually want to add an entire new internal command to the bash source code.
That way, when I compile it, it has my new command built in to the shell itself.
What are the steps involved in doing this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This actually came up as part of an answer to a more specific question (how to get the evaluated
PS1output to a variable) and I thought the process deserved its own question and answer, one more generalised to the process of modifyingbashsource code.Here are the steps to add an internal command
evalps1, which is meant to give you the same output as your primary prompt string. It’s based on the version4.2code base.Step 1.
First, change
support/mkversion.shso that you won’t confuse it with a “real”bash, and so that the FSF can deny all knowledge for warranty purposes 🙂 Simply change one line (I added the-paxbit):That script is the one which creates
version.hand affects all the various ways of getting version information frombash:--version, the$BASH_VERSIONvariable and so forth.Step 2:
Change
builtins/Makefile.into add a new source file to the mix. This entails a number of steps.(a) Add
$(srcdir)/evalps1.defto the end ofDEFSRC. Thisdeffile contains information about the internal command as well as the implementation code.(b) Add
evalps1.oto the end ofOFILES. This simply causes your code to be linked in withbash.(c) Add the required dependencies:
Step 3:
Add the
builtins/evalps1.deffile itself, this is the code that gets executed when you run theevalps1command:The bulk of it is the GPL licence (since I modified it from
exit.def) as shown above, with a very simple function at the end to get and decodePS1, using the same functions as the shell itself uses:The code itself is quite simple in this case, although it uses what I consider unnecessarily complicated indentation/brace rules and a K&R-style pre-prototype function declaration – I’ve kept them the same as the current code for consistency.
The code just calls the function to get the
PS1environment variable, then calls another function which decodes prompt strings. Finally, it outputs the decoded string to standard output.Of course, you may replace that code with your own (along with changing the command and file names from
evalps1to something else), depending on your particular needs.Step 4:
Just build the thing in the top level directory:
The
bashexecutable that appears in that top-level directory can be renamed topaxsh(for example), though I doubt it will ever become as prevalent as its ancestor 🙂And running it, you can see it in action, starting in a real
bashshell: