Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:20:49+00:00 2026-06-01T11:20:49+00:00

I don’t mean an alias or function. I actually want to add an entire

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:20:50+00:00Added an answer on June 1, 2026 at 11:20 am

    This actually came up as part of an answer to a more specific question (how to get the evaluated PS1 output to a variable) and I thought the process deserved its own question and answer, one more generalised to the process of modifying bash source 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 version 4.2 code base.

    Step 1.

    First, change support/mkversion.sh so 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 -pax bit):

    echo "#define DISTVERSION \"${float_dist}-pax\""
    

    That script is the one which creates version.h and affects all the various ways of getting version information from bash: --version, the $BASH_VERSION variable and so forth.

    Step 2:

    Change builtins/Makefile.in to add a new source file to the mix. This entails a number of steps.

    (a) Add $(srcdir)/evalps1.def to the end of DEFSRC. This def file contains information about the internal command as well as the implementation code.

    (b) Add evalps1.o to the end of OFILES. This simply causes your code to be linked in with bash.

    (c) Add the required dependencies:

    evalps1.o: evalps1.def $(topdir)/bashtypes.h $(topdir)/config.h \
               $(topdir)/bashintl.h $(topdir)/shell.h common.h
    

    Step 3:

    Add the builtins/evalps1.def file itself, this is the code that gets executed when you run the evalps1 command:

    This file is evalps1.def, from which is created evalps1.c.
    It implements the builtin "evalps1" in Bash.
    
    Copyright (C) 1987-2009 Free Software Foundation, Inc.
    
    This file is part of GNU Bash, the Bourne Again SHell.
    
    Bash is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Bash is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
    
    $PRODUCES evalps1.c
    
    $BUILTIN evalps1
    $FUNCTION evalps1_builtin
    $SHORT_DOC evalps1
    Outputs the fully interpreted PS1 prompt.
    
    Outputs the PS1 prompt, fully evaluated, for whatever nefarious purposes
    you require.
    $END
    

    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 decode PS1, using the same functions as the shell itself uses:

    #include <config.h>
    #include "../bashtypes.h"
    #include <stdio.h>
    #include "../bashintl.h"
    #include "../shell.h"
    #include "common.h"
    
    int
    evalps1_builtin (list)
         WORD_LIST *list;
    {
      char *ps1 = get_string_value ("PS1");
      if (ps1 != 0)
      {
        ps1 = decode_prompt_string (ps1);
        if (ps1 != 0)
        {
          printf ("%s", ps1);
        }
      }
      return 0;
    }
    

    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 PS1 environment 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 evalps1 to something else), depending on your particular needs.

    Step 4:

    Just build the thing in the top level directory:

    ./configure
    make
    

    The bash executable that appears in that top-level directory can be renamed to paxsh (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 bash shell:

    pax> mv bash paxsh
    
    pax> ./paxsh --version
    GNU bash, version 4.2-pax.0(1)-release (i686-pc-linux-gnu)
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    
    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    pax> ./paxsh
    
    pax> echo $BASH_VERSION
    4.2-pax.0(1)-release
    
    pax> echo "[$PS1]"
    [pax> ]
    
    pax> echo "[$(evalps1)]"
    [pax> ]
    
    pax> PS1="\h: "
    
    paxbox01: echo "[$PS1]"
    [\h: ]
    
    paxbox01: echo "[$(evalps1)]"
    [paxbox01: ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't be scared of the extensive code. The problem is general. I just provided
I don't know if this is a well known 'thing' or something new in
I don't want to know a way to preload images, I found much on
Don't know if I worded the question right, but basically what I want to
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
Don't know much about running a function on every item in an array, still
Don't these two mean the same thing, first get the value and then increment?
Don't know what to do with this error. How to add data in SQL
Don't get me wrong, I want them to get saved. But I always thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.