Tag Info

Hot answers tagged

13

The tutorial is wrong. POSIX says: A single-quote cannot occur within single-quotes. Here's some alternatives: echo $'It\'s Shell Programming' # ksh, bash, and zsh only, does not expand variables echo "It's Shell Programming" # all shells, expands variables echo 'It'\''s Shell Programming' # all shells, single quote is outside the quotes echo ...


7

You need to quote the variables and avoid the command substitution: for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec copy -acodec copy "${i}.mp4"; done See When is double-quoting necessary? for a detailed explanation of quoting. While I'm at it, the above produces files with a .mkv.mp4 extension; to fix that: for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec ...


4

If FS is longer than a single character, it is treated as a regular expression. An FS of just * is seen as a fixed string, but an FS of -*- is a regular expression, and -*- is equivalent to -+ (one or more -). So you need to make * be considered as a regular character. -\*- and -[*]- can both do this. However, the string for FS is parsed twice - once when ...


3

Aside from jasonwryan's suggestion, I'd suggest using printf: $ printf "%s http://ftp.cn.debian.org/debian/ wheezy main contrib non-free\n" deb deb-src > test $ cat test deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free Since printf reuses the format string until ...


3

A key point in muru's answer is that to get a backslash into the FS regex you need to write double backslash \\. That's because backslash is used as an escape character at two different levels. A single backslash in a string will be treated as escaping the following character, so we need to escape the backslash itself so that we get a single backslash in ...


2

You can use the wildcard * for the name that you don't know. Let's say you set FILE=../"$1"/*.txt Wildcard expansion only happens outside of quotes. $FILE variable still has the asterisk in it. So, [ -f "$FILE" ] is equivalent to [ -f "../test/*.txt" ] which wants a literal asterisk in the filename, but [ -f $FILE ] is equivalent to [ -f ...


2

You don't want the single quotes to get propagated to the makefile. Don't quote them, and replace them with double quotes - that would keep the variable value as one word in the shell. make -f make_gene_read_count.mk -n SAMPLES_OUT="$SAMPLES_out" If you want to pass more variables, separate them by unquoted whitespace: make VAR1="some value containing ...


2

The direct answer to your question is "use double quotes" because single quotes prevent all expansions: right=$(echo "$wrongpath" | sed "s|$oldtargetdir|$goodtargetdir|") There's no need for the trailing semicolon; they're only necessary when something follows on the same line (so the one before done is not redundant, though the layout is unorthodox and ...


2

Variables are not expanded in single quotes, but they are in double quotes. Moreover, you don't need sed for such a simple substitution, you can use parameter expansion: right=${wrongpath/$oldtargetdir/$goodtargetdir}


2

POSIXly you can safely escape any string into one concatenated string for reinput to the shell like: alias "string=$(cat file)"; alias string alias will hard-quote its output and prepend (at least) string= to head of the string. bash (in a break with the standard) also adds the string alias to head of the output. Still, you can get an eval-friendly quoted ...


2

If you only want to escape every double quotation mark and backslash you could use perl -wpe 's/([\\"])/\\$1/g' You could also use this with xclip: cat myfile | perl -wpe 's/([\\"])/\\$1/g' | xclip -selection clipboard


2

You put quoting chars into a string. The problem is that quoting chars are not parsed after parameter expansion. In order to get the desired effect you would need eval sed ... $drill_file You can use set -x to see how the shell sees the command line: > text=a\\\ \\\ \\\ b > echo $text a\ \ \ b > set -x > : $text + : 'a\' '\' '\' b I.e. the ...


2

You don't need to do the first sed if you quote your variables: read -p "Give relative or absolute path to the \"drill.TXT\" file: " drill_file if [[ -f "$drill_file" ]]; then sed 's/\(^X[[:digit:]]*\)[[:digit:]]\(Y[[:digit:]]*\)[[:digit:]]/\1\2/' "$drill_file" echo "Conversion finished." else echo "no such file: '$drill_file'" fi


2

Just include an actual newline inside the quotes (this works with either single or double quotes). Note that if the second line is indented, the spaces are part of the string. Furthermore always use double quotes around variable substitutions. str="deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free deb-src http://ftp.cn.debian.org/debian/ ...


2

The problem was that since the su statement was in double quotes, the variables were all expanded before the su command was called, which means $var becomes "cat" but $i becomes "" since it was not defined. Bash doesn't know that it was supposed to be an iterator variable, it just expands it to a NULL string. The answer is to escape that "$" like so: ...


1

The special parameter ${array[@]} in double quotes causes word splitting if the array has more than one member: $ for word in "for i in ${arr[@]} ; do" ; do echo "$word" ; done for i in a b c ; do If your modules' names don't contain spaces, you can have more luck using the * subscript: $ for word in "for i in ${arr[*]} ; do" ; do echo "$word" ; done for ...


1

To search for a parenthesis character, pass backslash+parenthesis to ack. Both backslash and parentheses are special in the shell, so you need to quote them when you're entering them in a shell script or on the command line. The simplest form of quoting is with single quotes: this tells the shell to pass everything through literally except single quotes ...


1

Variables are interpreted in a here doc (<<...), so you need to escape the ones you don't want evaluated yet. su - db2prd<<EOF PARMDATE=1111111 echo parmdate echo \$PARMDATE EOF Or better, quote the delimiter (here EOF) to tell your shell not to perform expansions inside the here-document: su - db2prd<<'EOF' PARMDATE=1111111 echo ...


1

The easiest thing is to put this at the top of your script: set -- $* This will then re-expand the parameter list, so any spaces in the parameters will become separators between new parameters. There may be some complicated behavior if you have very convoluted quoting in your parameters (e.g., quoted quotes), but my guess is that that is not likely to ...


1

In the first two examples you quote your argument. Those quotes are only seen by your shell, which treats the whole string as a single argument instead of splitting it on the spaces. The quotes itself are not seen by the program. When typing OPTSTR="'key1=val 1,key2=val 2,key3=val 3'" the content of your variable contains the single quotes, and will ...


1

Within the " delimiter, you need to escape the backslash one mre time. $ echo 'a -*- b' | awk 'BEGIN {FS="-\\*-"} {print $2}' b Since we are passing a regex to the FS variable, \\ within the double quotes is parsed as single backslash and then it apply the resultant regex against the input string.


1

A single-quoted string will retain the string as a literal. A double-quoted string will retain the string with variable interpolation and expansion. This is explained in the bash man page - see the section titled QUOTING There are three quoting mechanisms: the escape character, single quotes, and double quotes. A non-quoted backslash (\) is the ...


1

See the section on quoting in the Bash Reference Manual. Basically, enclosing characters within single or double quotes turns them into literal characters, with no special meaning (there are a few exception for double quotes but they don't matter here). So in the character sequence '"hey"', the single quotes "protect" all the other characters, and the ...


1

Here is escaped command: alias mm='ps -u $USER -o pid,rss,command | \ awk '\''{print $0}{sum+=$2} END {print "Total", sum/1024, "MB"}'\' Example of escaping quotes in shell: $ echo 'abc'\''abc' abc'abc $ echo "abc"\""abc" abc"abc It's simply done by finishing already opened one ('), placing escaped one (\'), then opening another one ('). ...



Only top voted, non community-wiki answers of a minimum length are eligible