Bash command line escape

Escape Exclamation Mark in Bash Command Line

This article introduces how to escape ! (exclamation mark) in Bash command line in Linux PC/server environments. I ran into the exclamation mark barrier. ! itself isn't used much normally, but it's a quite convenient command. It's well known that you can view history with ctrl+r…

Shou Arisaka
1 min read
Nov 14, 2025

This article introduces how to escape ! (exclamation mark) in Bash command line in Linux PC/server environments. I wanted to create a bash function that returns markdown when given an image, but I ran into the exclamation mark barrier.

! itself isn’t used much normally, but it’s a quite convenient command. It’s well known that you can view history with ctrl+r, but for example, if you execute pwd and then execute !p or !pw, pwd will be executed. It’s like that. However, escaping this was quite tricky.

bash - How to escape ! exclamation mark in Bash (140827)|teratail

Question

How do you escape ! exclamation mark in Bash?

$ echo "\![$(echo "hgoe")]($(echo "hgoe"))"
\![hgoe](hgoe)

I’m escaping with , but even \ is being output…

Answer

echo '!'"[$(echo "hgoe")]($(echo "hgoe"))"

Impressions

So in bash, you can concatenate strings with ” and "". I didn’t know that. Normally you can’t do that, right? It’s tricky, or should I say. Normally you’d think to do something like echo "" + ” ;, but this is unique to command line.

Share this article

Shou Arisaka Nov 14, 2025

🔗 Copy Links