568

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or string into the script. How do I approach doing this?

1

8 Answers 8

411

The shell command and any arguments to that command appear as numbered shell variables: $0 has the string value of the command itself, something like script, ./script, /home/user/bin/script or whatever. Any arguments appear as "$1", "$2", "$3" and so on. The count of arguments is in the shell variable "$#".

Common ways of dealing with this involve shell commands getopts and shift. getopts is a lot like the C getopt() library function. shift moves the value of $2 to $1, $3 to $2, and so on; $# gets decremented. Code ends up looking at the value of "$1", doing things using a caseesac to decide on an action, and then doing a shift to move $1 to the next argument. It only ever has to examine $1, and maybe $#.

2
  • 10
    the C getopt() is an extremely established standard, but the getopt executable is not the same across distro/platform. I'm a now fully converted getopts user, as it is a POSIX standard. It works great with dash also, which is my preferred script shell Commented Feb 10, 2012 at 23:40
  • 7
    Note also that $@ holds all arguments, as explained in this answer.
    – not2savvy
    Commented Jul 1, 2022 at 10:24
369

You can access passed arguments with $n where n is the argument number - 1, 2, 3, .... You pass the arguments just like you would with any other command.

$ cat myscript
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
$ ./myscript hello world
First arg: hello
Second arg: world
3
  • 1
    I can not get the right result from yours. The following is mine: ''' #!/bin/bash echo 'First arg:' $1 echo 'Second arg:' $2 ''' The shell name is para_script.sh Then execute it: ''' ./para_script.sh hello world
    – Newt
    Commented Jan 15, 2020 at 15:14
  • @Newt try running bash ./para_script Commented Jan 22, 2021 at 15:26
  • 1
    this is a bash script not a shell script
    – The Fool
    Commented Apr 22, 2021 at 7:17
151

On a bash script, I personally like to use the following script to set parameters:

#!/bin/bash

helpFunction()
{
   echo ""
   echo "Usage: $0 -a parameterA -b parameterB -c parameterC"
   echo -e "\t-a Description of what is parameterA"
   echo -e "\t-b Description of what is parameterB"
   echo -e "\t-c Description of what is parameterC"
   exit 1 # Exit script after printing help
}

while getopts "a:b:c:" opt
do
   case "$opt" in
      a ) parameterA="$OPTARG" ;;
      b ) parameterB="$OPTARG" ;;
      c ) parameterC="$OPTARG" ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done

# Print helpFunction in case parameters are empty
if [ -z "$parameterA" ] || [ -z "$parameterB" ] || [ -z "$parameterC" ]
then
   echo "Some or all of the parameters are empty";
   helpFunction
fi

# Begin script in case all parameters are correct
echo "$parameterA"
echo "$parameterB"
echo "$parameterC"

With this structure, we don't rely on the order of the parameters, as we're defining a key letter to each one of them. Also, the help function will be printed all the times that the parameters are defined wrongly. It's very useful when we have a lot of scripts with different parameters to handle. It works as the following:

$ bash myscript -a "String A" -b "String B" -c "String C"
String A
String B
String C

$ bash myscript -a "String A" -c "String C" -b "String B"
String A
String B
String C

$ bash myscript -a "String A" -c "String C" -f "Non-existent parameter"
myscript: illegal option -- f

Usage: myscript -a parameterA -b parameterB -c parameterC
    -a Description of what is parameterA
    -b Description of what is parameterB
    -c Description of what is parameterC

$ bash myscript -a "String A" -c "String C"
Some or all of the parameters are empty

Usage: myscript -a parameterA -b parameterB -c parameterC
    -a Description of what is parameterA
    -b Description of what is parameterB
    -c Description of what is parameterC
4
  • Does this work on centos?
    – Sai Nikhil
    Commented Mar 25, 2020 at 9:43
  • @saint1729 Sure, it works on any operating system (even on Windows) that you have with bash installed, you just need to execute it. Commented Mar 26, 2020 at 15:22
  • 14
    Cool. It worked for me. But, the key needs to be only one character. I wasted almost an hour figuring this out.
    – Sai Nikhil
    Commented Mar 26, 2020 at 15:40
  • Great example, wasn't clear to me what the single characters referred to at first. Another example for those stuck: while getopts "f:b:k:a:s:" opt do case "$opt" in f ) parameterFilepath="$OPTARG" ;; b ) parameterBucket="$OPTARG" ;; k ) parameterKey="$OPTARG" ;; a ) parameterAccessKey="$OPTARG" ;; s ) parameterSecretKey="$OPTARG" ;; ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent esac done
    – chris
    Commented Jun 10, 2021 at 10:00
45

The form

$ ./script.sh "$@" 

is the most convenient for argv. The arg delimiter is whitespace, but can be changed. Don't remember how off-hand. Then use

 $1, $2, shift, if [ $# -ne 3 ] 

to control flow. I don't usually embrace getopts if a case ... esac will suffice.

40
$/shellscriptname.sh argument1 argument2 argument3 

You can also pass output of one shell script as an argument to another shell script.

$/shellscriptname.sh "$(secondshellscriptname.sh)"

Within shell script you can access arguments with numbers like $1 for first argument and $2 for second argument and so on so forth.

More on shell arguments

29
./myscript myargument

myargument becomes $1 inside myscript.

12

In the shell script ; it becomes the variable name $1 . The second word becomes the variable name $2 and so on .

cat << 'EOF' > test
echo "First arg: $1"
echo "Second arg: $2"
echo "List of all arg: $@"
EOF
sh test hello world

More maybe found in the shell (sh) manual at http://heirloom.sourceforge.net/sh/sh.1.html

1
  • 4
    welcome to U&L, this hardly add anything to already given answer.
    – Archemar
    Commented Sep 20, 2017 at 13:26
4

The following is mine:

#!/bin/bash 
echo 'First arg:' $1 
echo 'Second arg:' $2 

The shell name is para_script.sh

Then execute it: ./para_script.sh hello world

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.