Deciphering Bash Parameters for Expansion: An In-Depth Look

Bash parameters play a pivotal role in enhancing shell scripting by storing values for later reference. Various symbol combinations like "$@", "$!", etc., represent different types of parameter expansions active in the shell. Let’s delve into these and understand their purpose with examples.

  1. $@all parameters

Represents all of the input parameters or arguments passed to a script.

For example, consider a Bash script that prints all the arguments passed.

$ cat script.sh
#!/bin/bash
echo "$@"

$ ./script.sh arg1 arg2
arg1 arg2
  1. $*merged parameters

Similar to "$@", but rather than maintaining the original word splitting, it merges all positional parameters into one.

Here’s an example of a script using $@:

$ cat script.sh 
#!/bin/bash
echo "$*"

$ ./script.sh arg1 arg2
arg1 arg2

3. $#number of arguments

Outputs the total number of arguments passed to the script.

Here’s an example of a script using $#:

$ cat script.sh 
#!/bin/bash
echo "$#"

$ ./script.sh arg1 arg2
2
  1. $!process id

Displays the process ID of the most recently executed background pipeline. This is useful for tracking and signaling processes.

$ cat script.sh
#!/bin/bash
sleep 100 &
echo "$!"

$ ./script.sh
7059

Running this script would output the process ID of the sleep 100 & command.

  1. $0script name

Represents the name of the script itself.

Adding echo "$0" to a script would display the script’s name when executed:

$ cat script.sh 
#!/bin/bash
echo "$0"

$ ./script.sh
./script.sh
  1. $1, $2, ..., $npositional arguments

These provide access to the arguments passed to the script, with the numbers correlating to the argument’s position. "$1" would be the first argument, "$2" the second, and so forth.

$ cat script.sh
#!/bin/bash
echo "$1"
echo "$2"
echo "$3"

$ ./script.sh Test One Two
Test
One
Two

Working in the Shell

Alongside their use in scripting, Bash parameters can also prove quite valuable during direct shell interactions. For example, let’s explore the usage of "$!" in a shell session.

Executing the "$!" command is known as the ‘bang-bang’ command in shell. It reruns the last executed command.

Consider the following shell session:

$ touch myfile.txt
$ ls
myfile.txt otherfile.txt
$ rm myfile.txt
$ !!
rm myfile.txt

In this scenario, the ‘!!’ command re-executes the last command – in this case, rm myfile.txt.

Now, let’s explore the use of "!$" which recalls the last argument from the previous command.

$ touch newfile.txt
$ ls -l !$
ls -l newfile.txt
-rw-r--r--  1 user  staff  0 Mar 26 17:41 newfile.txt

In this case "!$" recalls ‘newfile.txt’ from the touch newfile.txt command and reuses it as the argument for ls -l.

Finally, combining shell parameter expansions can significantly boost our efficiency on the command line. Remember to use these tools to aid your shell sessions, tighten your workflow, and elevate your productivity in Linux environments. We hope this guide has shed light on these powerful Bash expansions!