Android Backstage, a podcast by and for Android developers. Hosted by developers from the Android engineering team, this show covers topics of interest to Android programmers, with in-depth discussions and interviews with engineers on the Android team at Google. Subscribe to Android Developers YouTube → https://goo.gle/AndroidDevs
…
continue reading
תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.
Player FM - אפליקציית פודקאסט
התחל במצב לא מקוון עם האפליקציה Player FM !
התחל במצב לא מקוון עם האפליקציה Player FM !
HPR3071: Bash snippet - quotes inside quoted strings
Manage episode 446211220 series 2795599
תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.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 פרקים
Manage episode 446211220 series 2795599
תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.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 פרקים
כל הפרקים
×ברוכים הבאים אל Player FM!
Player FM סורק את האינטרנט עבור פודקאסטים באיכות גבוהה בשבילכם כדי שתהנו מהם כרגע. זה יישום הפודקאסט הטוב ביותר והוא עובד על אנדרואיד, iPhone ואינטרנט. הירשמו לסנכרון מנויים במכשירים שונים.