Tag Info

New answers tagged

11

Without quotes the string is subject to word splitting and globbing. See also BashPitfalls #14. Compare $ echo $(printf 'foo\nbar\nquux\n*') foo bar quux ssh-13yzvBMwVYgn ssh-3JIxkphQ07Ei ssh-6YC5dbnk1wOc with $ echo "$(printf 'foo\nbar\nquux\n*')" foo bar quux * When word splitting occurs the first character of IFS acts as a separator (which, per ...


4

The likely most important difference would be if the directory that the script is in has a space in it. In that case, the first line, the one without the double-quotes, would fail. This would be the result of "word splitting" which bash does on unquoted strings. Suppose that the result of dirname ${BASH_SOURCE[0]} is /home/j r/bin. Consider the line ...


6

The main difference is that the quoted version is not subject to field splitting by the shell. With double quotes the outcome of the command expansion would be fed as one parameter to the source command. Without quotes it would be broken up into multiple parameters, depending on the value of IFS which contains space, TAB and newline by default. If the ...


2

Use backslashes to protect $ and " inside the remote command: ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && \$1!=\"Destination\" {printf \"%-15s %-20s\n\", \$1, \$2}'|sort -f "


6

use here-docs to get around all of the nasty subshell quoting: ssh you@host <<-\SSH awk -f 3<<\AWK /dev/fd/3 awk script as many lines as you like "$vars and quotes" are only evaluated by awk #END AWK "$vars and quotes" are only evaluated by remote shell echo 'single quotes and all' rest of ssh ...


0

You can use rename for such tasks: ➜ lab ls 1dsfa.file 6033dsfa.file 90843_O\\'ConnorPaul_GeneralManager.jpg 56dsfa.file 90843_O\'ConnorPaul_GeneralManager.jpg ➜ lab rename "s/\'//g" * ➜ lab ls 1dsfa.file 6033dsfa.file ...


-1

Grrr, @Graeme beat me to it, but I'll post anyway ... If you quote the filename with double quotes, like mv -v "90843_O'ConnorPaul_GeneralManager.jpg" 90843_O_ConnorPaul_GeneralManager.jpg, it does the trick; using -v is optional but it'll tell what it did. I believe there's a second way too apart from escaping all the chars, -if- that even is possible. The ...


2

Bash tab completion should be able to do the right thing here if you just type mv 90843_O and press tab. Otherwise, one way to properly escape the name is: mv "90843_O\\\\'ConnorPaul_GeneralManager.jpg" dest.jpg The double quotes remove the need to escape the ', but the two backslash characters still need to be escaped (one extra backslash for each makes ...


2

You should always(*) quote your variables, particularly if they contain arbitrary user input. In this case, if you're scripting with bash, use double brackets so you don't need to quote (bash is smart that way), and use printf "%q" to get bash to escape what needs to be escaped: ASOURCE="Home Photos 2012 camcorder" #example filename if [[ -d $ASOURCE ]]; ...


2

The rules are different for single quotes versus double quotes. For the reason you show, double quotes can't be used reliably in bash, because there's no sane way to escape an exclamation mark. $ grep -oP "\\(.*(?!word).*right" bash: !word: event not found $ grep -oP "\\(.*(?\!word).*right" grep: unrecognized character after (? or (?- The second is ...


3

If you want to match from the second open parentheses up until (but not including) the next closing parentheses: grep -Po '\(.*?\K\([^)]*' Or portably with sed: sed -n 's/^[^(]*([^(]*\(([^)]*\).*/\1/p' To match the right most ( that is not followed by word up to the rightmost right after that: grep -Po '.*\K\((?!word).*right'


0

Look for the comma parens before and aft instead: grep -oP '(?<=,)\(.*(?=\),)' Example $ echo '(foo),(bar,baz(word,right),(end)' | grep -oP '(?<=,)\(.*(?=\),)' (bar,baz(word,right The lookahead and behind can only look for explicit strings, it cannot find things such as .*. References Lookahead and Lookbehind Zero-Length Assertions


2

You can do it with simple awk: $ echo '(foo),(bar,baz(word,right),(end)' | awk -F'),' '{print $2}' (bar,baz(word,right


1

You need to tell the shell that you mean for the braces and spaces to be part of the file name, instead of being interpreted as shell syntax. You can type file names containing special characters inside single quotes. This works for every character except the single quote itself. This is the easiest method in a script. mv '{{ THEME SANITIZED }}.hacks.css' ...


1

you can use double quotes to scape or scape with slash like for example: using double quotes: mv "{{ THEME SANITIZED }}.hacks.css" myomega.hacks.css using slash: mv \{\{\ THEME\ SANITIZED\ \}\}.hacks.css myomega.hacks.css something cool that some people doesn't know how to remove ou rename a directory when it's start with dash you can you dash dash :D ...


3

mv "{{ THEME SANITIZED }}.hacks.css" myomega.hacks.css will work.


3

You are using single quotes in your sed expression which would prevent variable expansion. Use double quotes. Moreover, use a different separator than /: sed "s|{path}|$(pwd)|g" apache-vhost.conf Moreover, cat file | sed is a useless use of cat.


1

You must use an dolar sign $ in assignment to TIMEFORMAT: TIMEFORMAT=$'\n\nreal_test\t%5lR\nuser_test\t%5lU\nsys_test\t%5lS' time ls .... real_test 0m0.006s user_test 0m0.000s sys_test 0m0.004s


2

The shell does not interpret any backslashes inside single quotes. If you want backslashes to be interprested use the $'...' construct, as in: TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS' From man bash: Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as ...


5

You need to ensure the loop vars are derefenced on the remote host: ssh -t -t $username@$sourceIP 'bash -s' <<ENDSSH IFS='-' for sourceP in $port; do for destinationP in $destinationPorts; do echo "\$sourceP" - "\$destinationP" done done ENDSSH Using all the parentheses launches a subshell for each loop iteration, which is a lot of ...


3

That's not how quoting work in bash. When entering ls "-R" quotes removal happens on the command, so the command becomes ls -R If, on the other hand, if you set text='ls "-R"' and run $text, the quote removal does not happen. See man bash under EXPANSIONS: After the preceding expansions, all unquoted occurrences of the characters \, ', and " ...


3

The wrong quoting is the problem. " does not quote (and, which is the problem here, does not get removed!) if it is contained in a variable. Exception: eval $text You should assign the parameters to an array and use ls "${vars[@]}" or ls "$@" instead. Compare the two calls: set -x ls -l ls "-l" set +x



Top 50 recent answers are included