Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr3071 :: Bash snippet - quotes inside quoted strings

How to add quotes to quoted strings in Bash

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2020-05-11 is flagged as Explicit and is released under a CC-BY-SA license.
Bash, quotes. 2.
The show is available on the Internet Archive at: https://archive.org/details/hpr3071

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:13:19

Bash Scripting.

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Bash and quoted strings

An issue I just hit in Bash was that I had a quoted string, and I wanted to enclose it in quotes. How to do this?

This is the umpteenth time I have stumbled over this issue, and I realised I had found out how to solve it a while back but the information hadn’t rooted itself into my mind!

I have always been less clear in my mind about quoted strings in Bash than I should be, so, assuming others might have similar confusion I thought I’d try and clarify things in the form of an HPR show.

The problem

The thing I was having difficulties with was an alias definition of a useful pipeline:

nmap -sn 192.168.0.0/24 | awk '/^Nmap scan report/{print ""; print; next}{print}'

This uses nmap (see Ken’s show 3052 for a discussion of its use) piped into an awk one-liner that formats the information returned by nmap.

The alias command can be used to store such a command or command sequence as a single simple command. It’s usually added to the ~/.bashrc file so it gets added to every Bash shell you start up (note Bash Tips #22, currently being written, will cover these startup files).

An alias definition looks something like this:

alias la='ls -Al'

The alias itself 'la' is defined as the command ls -Al.

So how to make my nmap sequence into an alias given that the commands contain both single and double quotes?

Quoted strings in Bash

Bash is (to my mind) a bit weird with quoted strings.

There are two sorts of quotes in Bash (leaving aside the backquote or backtick`):

  • Single quotes, also called hard quotes ('). The literal value of characters between the quotes is preserved. Single quotes are not allowed, even if preceded by backslash escape characters.

  • Double quotes, also called soft quotes ("). Certain characters within the quotes have special meanings, such as '$' and '\'. Double quotes are allowed in the string when preceded by a backslash.

There’s a more comprehensive treatment of these quoting types (and others) in the Bash Reference Manual.

Changing quotes and concatenating strings

To make a variable containing a string with embedded quotes you can do this:

$ x='string1'"'"'string2'
$ echo $x
string1'string2

What we did here was close 'string1', start a new string enclosed in double quotes "'", then append a second string 'string2'. Bash treats the three strings as one, but they have to be contiguous. There must be no intervening spaces1.

This solution is rather ugly. You could also use Bash string concatenation to do this, though it’s more long-winded:

$ x='string1'
$ x+="'"
$ x+='string2'
$ echo $x
string1'string2

The same principles hold for double quotes of course:

$ x="string1"'"'"string2"
$ echo $x
string1"string2

You’d probably not want to do this though.

Using backslashes

You can use backslashes to escape double quotes inside a double quoted string in Bash as we have seen.

$ x="string1\"string2"
$ echo $x
string1"string2

However, as discussed earlier, it’s not possible to use backslashes to escape single quotes inside a single quoted string in Bash. However, outside a string a backslashed character is escaped. For example, if you have files which have spaces in their names, you can quote the name or use the backslash escape to protect the spaces2:

$ ls -l a\ file\ with\ spaces.awk
-rw-r--r-- 1 hprdemo hprdemo 0 Apr 22 22:25 'a file with spaces.awk'

So, knowing this, you can exit a string, concatenate with a backslashed quote then restart a string like this:

$ x='string1'\''string2'
$ echo $x
string1'string2

Solution

So now we can see how to achieve the alias definition I wanted earlier:

alias show_network='nmap -sn 192.168.0.0/24 | awk '\''/^Nmap scan report/{print ""; print; next}{print}'\'''

Epilogue

There’s more to be said about this subject, but too much of this stuff is not healthy.


  1. This is quite an artificial example to make a point. You wouldn’t do things this way in reality. Using x='string1'"'string2" would also work ('string1' in single quotes, and "'string2'" in double quotes). Also, you could just write x="string1'string2" and stop all the messing about, but that would not be much of an example!

  2. The backslash is making the space a literal space, otherwise Bash would see it as an argument delimiter, and would look for the files 'a', 'file', 'with' and 'spaces.awk' to list details about!


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2020-05-22 11:32:23 by nobody

Further simplifying

There are actually more than two types of quoting in bash. In addition to 'single' and "double there are also $'ANSI-C' and $"localized" quotations. For this problem I would have used the $'ANSI-C' quotation: alias show_network=$'nmap -sn 192.168.0.0/24 | awk '/^Nmap scan report/{print ""; print; next}{print}''

Personally I find this a bit more readable. It might not be as portable but that shouldn't matter as the episode specifically addresses Bash.

Here are the expansions for the ANSI-C quotation in the Bash manual: https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html

Comment #2 posted on 2020-05-22 16:44:12 by Dave Morriss

To 'nobody' re ANSI-C quoting

Thanks for this.

You are of course perfectly right. I tend not to think of this way of doing things - maybe because I originally learned Unix on HP-UX and SunOS a long time ago. I might be accused of being a little stuck in my ways!

I did think of mentioning this in the show but didn't do it because it was a "snippet" and I didn't want to go into too much detail and make the episode too long. I did link to the relevant page in the documentation (https://www.gnu.org/software/bash/manual/html_node/Quoting.html) however.

Feel free to add to the Bash Scripting series yourself. I look forward to hearing a different view from you in due course.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?