Artwork

Content provided by HPR Volunteer and Hacker Public Radio. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by HPR Volunteer and Hacker Public Radio or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://ppacc.player.fm/legal.
Player FM - Podcast App
Go offline with the Player FM app!

HPR3071: Bash snippet - quotes inside quoted strings

 
Share
 

Manage episode 446211220 series 2795599
Content provided by HPR Volunteer and Hacker Public Radio. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by HPR Volunteer and Hacker Public Radio or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://ppacc.player.fm/legal.
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 bac
  continue reading

116 episodes

Artwork
iconShare
 
Manage episode 446211220 series 2795599
Content provided by HPR Volunteer and Hacker Public Radio. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by HPR Volunteer and Hacker Public Radio or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://ppacc.player.fm/legal.
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 bac
  continue reading

116 episodes

All episodes

×
 
Loading …

Welcome to Player FM!

Player FM is scanning the web for high-quality podcasts for you to enjoy right now. It's the best podcast app and works on Android, iPhone, and the web. Signup to sync subscriptions across devices.

 

Quick Reference Guide

Copyright 2025 | Privacy Policy | Terms of Service | | Copyright
Listen to this show while you explore
Play