Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ HPR3071: Bash snippet - quotes inside quoted strings

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š HPR3071: Bash snippet - quotes inside quoted strings


๐Ÿ’ก Newskategorie: Podcasts
๐Ÿ”— Quelle: hackerpublicradio.org

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.

Links


  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!โ†ฉ

...



๐Ÿ“Œ HPR3713: Bash snippet - short-circuit evaluation in Bash Boolean expressions


๐Ÿ“ˆ 42.18 Punkte

๐Ÿ“Œ Back to bash: Inception, running bash inside bash


๐Ÿ“ˆ 41.43 Punkte

๐Ÿ“Œ Frog CMS 0.9.5 Edit Snippet admin/ snippet[name] cross site scripting


๐Ÿ“ˆ 41.11 Punkte

๐Ÿ“Œ Wolf CMS 0.8.3.1 Add SNippet /?/admin/snippet/add cross site scripting


๐Ÿ“ˆ 41.11 Punkte

๐Ÿ“Œ CVE-2023-23277 | Snippet-box 1.0.0 Form Field Snippet code cross site scripting (Issue 57)


๐Ÿ“ˆ 41.11 Punkte

๐Ÿ“Œ niieani/bash-oo-framework: Bash Infinity is a modern boilerplate / framework / standard library for bash


๐Ÿ“ˆ 32.43 Punkte

๐Ÿ“Œ Bash Scripting - BASH Scripts For Heavy Commandline Users [Bash Snippets]


๐Ÿ“ˆ 32.43 Punkte

๐Ÿ“Œ Bash Scripting - BASH Scripts For Heavy Commandline Users [Bash Snippets]


๐Ÿ“ˆ 32.43 Punkte

๐Ÿ“Œ Update: strings.py Version 0.0.5 Pascal Strings


๐Ÿ“ˆ 32.21 Punkte

๐Ÿ“Œ RE pt. 1 - A Strings Primer and Solving picoCTF pico strings


๐Ÿ“ˆ 32.21 Punkte

๐Ÿ“Œ HPR3722: Bash snippet - plurals in messages


๐Ÿ“ˆ 31.37 Punkte

๐Ÿ“Œ HPR3985: Bash snippet - be careful when feeding data to loops


๐Ÿ“ˆ 31.37 Punkte

๐Ÿ“Œ Pinephone updates, quoted from telegram group


๐Ÿ“ˆ 29.85 Punkte

๐Ÿ“Œ Dovecot up to 2.2.36.3/2.3.7.1 Quoted String Out-of-Bounds memory corruption


๐Ÿ“ˆ 29.85 Punkte

๐Ÿ“Œ โ€œI too like to live dangerouslyโ€, Accidentally Finding RCE in Signal Desktop via HTML Injection in Quoted Replies


๐Ÿ“ˆ 29.85 Punkte

๐Ÿ“Œ Quoted replies spotted in Microsoft Teams 2.0 on Windows 11


๐Ÿ“ˆ 29.85 Punkte

๐Ÿ“Œ Aligning the first line of a triple-quoted string in Python


๐Ÿ“ˆ 29.85 Punkte

๐Ÿ“Œ How to generate random strings with Bash UUID


๐Ÿ“ˆ 26.92 Punkte

๐Ÿ“Œ Bash Append Text To a Variable (Concatenate Strings)


๐Ÿ“ˆ 26.92 Punkte

๐Ÿ“Œ How to write strings in Bash for special characters


๐Ÿ“ˆ 26.92 Punkte

๐Ÿ“Œ [WIP] The Pure Bash Bible - Documenting pure bash ways to do various tasks.


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Semi-Linux Noob. What do the pro's prefer zshell/ohmyzsh OR Bash/bash-it?


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Sensible Bash : Small & opinionated selection of basic Bash configurations for a better command-line user experience


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Simple Bash Prompt โ€“ a fast, pretty, extendable and pure bash prompt


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Blind Bash - Obfuscate And Protect Your Bash Code


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Linux bash exit status and how to set exit status in bash


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Bash add pause prompt in a shell script ( bash pause command )


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Writing Bash Scripts that are not only Bash: Checking for Bashisms and testing with Dash


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ Bash-Oneliner: A collection of handy Bash One-Liners and terminal tricks


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ #!/bin/bash vs #!/usr/bin/env bash


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ GitHub - awesome-lists/awesome-bash: A curated list of delightful Bash scripts and resources.


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ How to Work with Directories in the Terminal with Bash [8 of 20] | Bash for Beginners


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ What are Variables in Bash? [14 of 20] | Bash for Beginners


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ How to Modify File Permissions in Bash [12 of 20] | Bash for Beginners


๐Ÿ“ˆ 21.62 Punkte

๐Ÿ“Œ How to Use Redirection and Pipelines in Bash [11 of 20] | Bash for Beginners


๐Ÿ“ˆ 21.62 Punkte











matomo