Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
I’m sure many of us must have seen an exclamation mark (!) in bash scripts. The exclamation mark serves a different purpose, depending on where it’s being used. It’s one of the powerful features of the Bash shell and can help to improve productivity.
In this tutorial, we’ll see the various uses of the exclamation mark in Bash.
In Bash, the exclamation mark (!) is used with the pound (#) symbol to specify the interpreter path. This usage is called “shebang” and is denoted as:
#!interpreter [arguments]
In shell scripts, we can use it to specify bash as an interpreter. Note that this only works as expected when inside a script. When executing directly in the terminal, exclamation marks have a special meaning that we’ll see later:
$ cat welcome.sh
#!/usr/bin/bash
echo "Welcome !!!"
Similarly, we can use it in a directive in a Python script to specify the python executable as the interpreter:
$ cat welcome.py
#!/usr/bin/python
print("Welcome !!!")
Note that this directive must be the first line in the script.
We can use the exclamation mark (!) to invert the exit status of a command. Let’s execute a command that will return a non-zero status:
$ ls non-existing-file.txt
ls: cannot access 'non-existing-file.txt': No such file or directory
$ echo $?
2
Next, let’s invert its return status:
$ ! ls non-existing-file.txt
ls: cannot access 'non-existing-file.txt': No such file or directory
$ echo $?
0
Now, let’s execute a command that returns a zero status code:
$ echo 'Hello, World'
Hello, World
$ echo $?
0
And invert its return status to non-zero:
$ ! echo 'Hello, World'
Hello, World
$ echo $?
1
Note that there’s a space between the exclamation mark (!) and the command.
Bash maintains the history of the commands executed in the current session. We can use the exclamation mark (!) to execute specific commands from the history.
We can use !! to execute the previous command:
$ echo 'Hello, World'
Hello, World
$ !!
echo 'Hello, World'
Hello, World
We can use the !n syntax to execute the nth command from the history. In this expression, n represents the command number from history. We can use the history command to list the commands from history:
$ history
...
...
...
124 echo 'Hello, World'
125 history
Let’s execute the 124th command:
$ !124
echo 'Hello, World'
Hello, World
We can use !string syntax to execute the command from our history that starts with the given string. Let’s execute the command that starts with echo:
$ !echo
echo 'Hello, World'
Hello, World
We can use the exclamation mark (!) to refer to arguments from the previously executed commands.
We can use !:n to refer to the nth argument from the previously executed command:
$ echo one two three four
one two three four
$ echo !:2
echo two
two
We can use !:^ to refer to the first argument from the previously executed command:
$ echo one two three four
one two three four
$ echo !:^
echo one
one
We can use !:$ to refer to the last argument from the previously executed command:
$ echo one two three four
one two three four
$ echo !:$
echo four
four
We can use !:* to refer to all arguments from the previously executed command:
$ echo one two three four
one two three four
$ echo !:*
echo one two three four
one two three four
We can configure Bash to stop script execution as soon as there’s a failure in command execution. We can achieve this via enabling the set -e option.
However, sometimes there’s a requirement that certain failures can be ignored. In such cases, we can use the exclamation mark (!) to override exit on failure behavior:
$ cat ignore-failure.sh
#!/bin/bash
set -e
! ls non-existing-file.txt
echo "Previous command's failure has been ignored."
Let’s execute the script and verify the result:
$ chmod +x ignore-failure.sh
$ ./ignore-failure.sh
ls: cannot access 'non-existing-file.txt': No such file or directory
Previous command's failure has been ignored.
In this tutorial, we discussed various practical examples that show the use of exclamation marks (!) in Bash. We can use these commands in day-to-day life to boost our productivity.