Different parameters available in Linux Bash Shell with examples

In this post, we will look at the different parameters available in Linux Bash shell.

These parameters are very useful in preparation of shell scripts and in general shell operations.

Lets have a look at these parameters one by one with an example.

1. $0 gives the name of the shell or shell script.

### On shell, it returns the shell name.
user@ngelinux$ echo $0
-bash
user@ngelinux$ 

user@ngelinux$ cat example1.sh 
#!/bin/bash
echo "hello this is \$0 value: $0"

### In script, $0 return the script name.
user@ngelinux$ ./example1.sh 
hello this is $0 value: ./example1.sh

2. $1, $2, $3, … are the positional parameters.

### The script prints $1, $2 and $3 value.
user@ngelinux$ cat example2.sh 
#!/bin/bash
echo "the vlaues of 3 parameters passed are: $1 $2 $3"
user@ngelinux$ 

user@ngelinux$ ./example2.sh firstvar secondvar thirdvar
the vlaues of 3 parameters passed are: firstvar secondvar thirdvar
user@ngelinux$ 

3. $$ returns pid of the current shell (not subshell).

user@ngelinux$ echo $$
897

### We can see $$ is the current shell process ID.
user@ngelinux$ ps | grep 897
  897 ttys015    0:00.01 -bash

### In Shell script, it returns the script process ID generated, not the shell PID.
user@ngelinux$ cat bash.sh
#!/bin/bash
echo "Current shell script process id is $$"
user@ngelinux$

user@ngelinux$ ./bash.sh
Current shell script process id is 934

user@ngelinux$ ps | grep 934
  936 ttys015    0:00.00 grep 934

4. $? outputs the most recent foreground pipeline exit status.

### If command succeeds, the exit status returned is 0.
user@ngelinux$ echo "hello"; echo $?
hello
0

### if command failed the exit status returned is 1.
user@ngelinux$ df /dev/sdc; echo $?
df: /dev/sdc: No such file or directory
1
user@ngelinux$

5. $! is the Process ID of the most recent background command.

user@ngelinux$ tail -f file1 &
[1] 951

### Now $! will return the recent background Process ID.
user@ngelinux$ echo $!
951

### We can verify using ps command.
user@ngelinux$ ps -ef| grep -i 951
1447583   951   897   0 12:11PM ttys015    0:00.00 tail -f file1

6. $@ is an array-like construct of all positional parameters, {$1, $2, $3 …}.

user@ngelinux$ cat bash.sh
#!/bin/bash
echo "Passed parameters are $@"
user@ngelinux$

user@ngelinux$ ./bash.sh 1 2 3
Passed parameters are 1 2 3

7. $* is the IFS expansion of all positional parameters, $1 $2 $3 ….

user@ngelinux$ cat bash.sh
#!/bin/bash
echo "Passed parameters are $@"
echo "Passed parameters are $*"
user@ngelinux$

user@ngelinux$ ./bash.sh 1 2 3 5353
Passed parameters are 1 2 3 5353
Passed parameters are 1 2 3 5353

Here we can see the output of $# and $* are same. Since both outputs the same positional parameters $1 $2 $3.
The difference is that $@ outputs “$1” “$2”, and $* outputs “$1 $2”.

8. $# is the number of positional parameters.

user@ngelinux$ cat bash.sh
#!/bin/bash
echo "Passed parameters are $@"
echo "Passed parameters are $#"
user@ngelinux$

user@ngelinux$ ./bash.sh
Passed parameters are
Passed parameters are 0

user@ngelinux$ ./bash.sh 9 5
Passed parameters are 9 5
Passed parameters are 2

9. $- current options set for the shell.

 user@ngelinux$ echo $-
himBH

Here himBH are the shell options which denotes: h means remember the location of commands, i is for interactive output, m for monitor mode, B is for brace expansion, and H is to enable history command expansion that starts from !.

10. $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS is the (input) field separator.

user@ngelinux$ echo hello hello2; echo $_
hello hello2
hello2

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments