How do i use arguments in my shell script?
-
Arguments to a shell script are what you pass it when you run the script. These are called Positional Arguments.
For example, $0 means the script itself, the name of the script on the command line is $0.
The next and first positional argument would be $1, There's also $2, $3, etc
The script we want to run is as follows. nothing complicated here. We are passing the argument $1 to the script.
$ cat myfruit.sh #!/bin/bash echo My favourite fruit is $1
The result after running the script is:
$ myfruit.sh pineapple My favourite fruit is pineapple
On line one I provided the positional argument for $1 as pineapple.
© Lightnetics 2024