recentpopularlog in

kme : quoting   19

json - xargs: how to have literal double-quotes in replacement? - Stack Overflow
GNU Parallel was built specifically to deal with xargs bad handling of special chars:
<code class="language-bash"><source parallel curl -X POST -H "application/json" myURL -d {}</code>
shellscripting  sysadmin  xargs  gnuparallel  quoting  woes 
september 2019 by kme
Bash Reference Manual | https://www.gnu.org/
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
bash  shell  shellscripting  ansicquoting  quoting  dammitbrain  reference 
may 2019 by kme
Is it possible to create a multi-line string variable in a Makefile - Stack Overflow | https://stackoverflow.com/
This works fine, for example, to embed Python code in a Makefile; just stick to one type of quotes in the Python source, 'export' the 'define'd variable, and wrap it in the other kind of quote when you call 'python -c "$$PYSCRIPT"' from one of your recipes.
Yes, you can use the define keyword to declare a multi-line variable, like this:
<code class="language-makefile">
define ANNOUNCE_BODY
Version $(VERSION) of $(PACKAGE_NAME) has been released.

It can be downloaded from $(DOWNLOAD_URL).

etc, etc.
endef
</code>

The tricky part is getting your multi-line variable back out of the makefile. If you just do the obvious thing of using "echo $(ANNOUNCE_BODY)", you'll see the result that others have posted here -- the shell tries to handle the second and subsequent lines of the variable as commands themselves.

However, you can export the variable value as-is to the shell as an environment variable, and then reference it from the shell as an environment variable (NOT a make variable). For example:
<code class="language-makefile">
export ANNOUNCE_BODY
all:
@echo "$$ANNOUNCE_BODY"

</code>
make  makefile  python  quoting  shellscripting  dammitbrain  solution 
november 2018 by kme
The Unix Shell's Humble If | https://robots.thoughtbot.com/
This is a really good article, and it mentions things like not quoting regexes in double-square-brace tests.
unix  linux  bash  shellscripting  test  conditionals  newbie  quoting  syntax  tutorial 
november 2017 by kme
bash - Multi-line, double quoted string triggers history expansion on subsequent single-quoted commands it gets piped to - Stack Overflow | https://stackoverflow.com/
Makes more sense now why things like `echo "Hi there!"` don't work, but `echo "Hi there! "` do.
It is not a bug.

After asking this in the bash-bugs mailing list, I got the following answer:
History expansion is explicitly line-oriented.

It doesn't know about shell state, especially shell quoting state, that spans lines.

It does know about vaguely shell-like quoting that's common across a large set of Unix utilities -- since the history and readline libraries are used outside the shell -- and that a double quote introduces a quoted string in which single quotes are not significant and don't inhibit history expansion.
Upvote for documenting this, but that's insane behavior. – tripleee Jun 20 '16 at 8:42
bash  quoting  historyexpansion  history  weirdness  annoyance  gotcha  solution  fuckina 
november 2017 by kme
Rich’s sh (POSIX shell) tricks
Sometimes it’s necessary to put a string in a shell-quoted form, for instance if it needs to be expanded into a command that will be evaluated with eval, written into a generated script, or similar. There are several methods, but many of them fail if the string contains newlines. Here is a version that works:
<code class="language-bash">quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; }</code>

This function simply replaces every instance of «'» (single quote) within the string with «'\''» (single quote, backslash, single quote, single quote), then puts single quotes at the beginning and end of the string. Since the only character whose meaning is special within single quotes is the single quote character itself, this is totally safe. Trailing newlines are handled correctly, and the single quote at the end doubles as a safety character to prevent command substitution from clobbering the trailing newlines, should one want to do something like:
<code class="language-bash">quoted=$(quote "$var")</code>

Counting occurrences of a character
<code class="language-bash">tr -dc 'a' | wc -c</code>

This will count the number of occurrences of the character “a”, by deleting all other characters. However, it’s not clear what tr -dc does when encountering noncharacter bytes in the input; POSIX is unclear on this matter and implementations likely differ. Foundational logicians will appreciate this as a practical real-world difficulty in working with set complements and the universal set.

Instead, try the following:
<code class="language-bash">tr a\\n \\na | wc -l</code>

The wc -l command counts the number of newline characters seen, so using tr to swap occurrences of “a” with newlines allows tr to count “a”s instead.
stupidtricks  posix  bash  bourne  shellscripting  workaround  reference  quoting  bourneshell 
september 2017 by kme
python - Passing HTML to template using Flask/Jinja2 - Stack Overflow
You can also declare it HTML safe from the code:

from flask import Markup
value = Markup('<strong>The HTML String</strong>')

Then pass that value to the templates and they don't have to |safe it.
python  flask  jinja2  html  quoting  escaping  webdevel  solution 
july 2016 by kme
Quotes and escaping [Bash Hackers Wiki]
\ANSI C like strings

Bash provides another quoting mechanism: Strings that contain ANSI C-like escape sequences. The Syntax is:

$'string'


Example: $'\t'

I think this is a feature of all POSIX-compliant shells?
bash  quoting  shellscripting  shell  programming  devel  reference 
march 2016 by kme
Quotes - Greg's Wiki
When Should You Quote?

The basic rule of thumb is that you should double-quote every expansion. This prevents unwanted word splitting and globbing. When in doubt, quote it.

There are a few cases where double quotes may be safely omitted:

On the right hand side of a simple assignment. You may write foo=$bar without quotes. This is POSIX compliant.

The word following a case keyword. You may write case $foo in ... safely. This is POSIX compliant.

Inside a [[ command, except on the right hand side of an = or == operator. [[ already suppresses word splitting and globbing, so you can write things like [[ -z $string ]] safely if you wish. However, be warned that [[ foo = $bar ]] and [[ foo = "$bar" ]] act differently (see patterns below). The [[ keyword is a Bash extension.

Use single quotes when protecting complex strings, especially ones that contain shell syntax which you don't want evaluated.
quoting  sysadmin  linux  devel  shellscripting  scripting  bash  bestpractices  reference  arrays  essential 
july 2015 by kme
unix - Check if a directory exists in a shell script - Stack Overflow
Remember to always wrap variables in double quotes when interpolating them in a bash script. Kids these days grow up with the idea that they can have spaces and lots of other funny characters in their directory names. (Spaces! Back in my days, we didn't have no fancy spaces! ;))

One day, one of those kids will run your script with $DIRECTORY set to "My M0viez" and you script will blow up. You don't want that. So use this.

if [ -d "$DIRECTORY" ]; then
# Will enter here if $DIRECTORY exists, even if it contains spaces
fi
getoffmylawn  shellscripting  quoting  bash  newbie 
may 2015 by kme
sort (Unix) - Wikipedia, the free encyclopedia
Sorting a file with tab separated values requires a tab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation[1][2] to specify the tab as a C escape sequence.

$ sort -k2,2 -t $'\t' phonebook
unix  shell  solution  quoting  sysadmin  shellscripting  bash 
november 2014 by kme

Copy this bookmark:





to read