1

I am trying to run the following bash script, but it does not display anything on the terminal window and i get no errors either. I am trying to display the currentid value to be shown on the terminal screen as the output while using the builtin function when I run it through the command line.

#! /bin/bash

currentid()
{
return 608

echo builtin currentid "$@"

}

currentid
  • 2
    Place echo statements at various points to see what is happening. Do you think anything in currentid will execute after return? – Joe P Jun 10 '17 at 21:56
  • 2
    @JoeP Better to run it with bash -x then clutter it up with echos – Grayson Kent Jun 10 '17 at 23:03
0

I am going to assume you are trying to get the pid of your script and use it as variable later on? If so, here are some tips:

  1. You don't need the builtin command as currentid is not a builtin as far as I know. You can see what builtin does like so:

    $ help builtin
    builtin: builtin [shell-builtin [arg ...]]
    Execute shell builtins.
    
    Execute SHELL-BUILTIN with arguments ARGs without performing command
    lookup.  This is useful when you wish to reimplement a shell builtin
    as a shell function, but need to execute the builtin within the function.
    
    Exit Status:
    Returns the exit status of SHELL-BUILTIN, or false if SHELL-BUILTIN is
    not a shell builtin..
    

    You can find a list of all the builtins with compgen -b.

  2. I am going to take a leap of faith here and guess that with currentid you are trying to get the pid or Process Id of the script. Please correct me if currentid refers to something else.

    To get the current pid of your script, you can use $$ or $BASHPID. You can learn more about the differences in the Bash Reference Manual. I am not sure why you are hard-coding a return of 608, but if you want to change the pid of your script to that, please clarify your question.

    With that in mind, your script would look like this:

    #!/bin/bash
    printf "%s" "$$"
    

    Which prints your current pid safely. But do keep in mind that you can call $$ or $BASHPID in your script later on for whatever you want.

Some more general tips:

Good Luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.