Tag Info

New answers tagged

2

Just quote it: dir1="directory 1" dir2="directory 2" tar cvpzf /destination/backup.tgz "$dir1" "$dir2" Or, if your shell supports it (bash, which you've tagged your question with, does but sh, which you are using in your script doesn't) use arrays: targets=( "directory 1" "directory 2" ) tar cvpzf backup.tgz "${targets[@]}"


2

use triple double quote ? if [ -e "./${name}/"""$1"""_page.json" ]; this will replace ${name} and $1 by actual values. please note: archemar@mybox:~/tmp7$ foo=bar archemar@mybox:~/tmp7$ f2=deux archemar@mybox:~/tmp7$ echo "${f2}/"""${foo}"""/test.php" deux/bar/test.php archemar@mybox:~/tmp7$ echo "${f2}/\"${foo}\"/test.php" deux/"bar"/test.php ...


0

Just use single-quotes. if [ -e './${name}/"$1"_page.json' ]; Note that if you want ${name} and $1 to be expanded, you can't use single quotes. Read this article for the difference between strong and weak quoting. However, if you want to expand arguments it's perfectly fine to concatenate strings: if [ -e './'"${name}"'"'"$1"'"_page.json' ]; Though ...


1

for HOST in $(grep -v '^#' db_hostlist2.txt) do echo "Checking for $HOST" ssh admin@$HOST <<EOF echo "Hostname : " \$(hostname) echo -e "Total DB is : \$(ps -ef | grep smon | grep -v grep | wc -l)\n" echo -e "Total DB is : \n\$(ps -ef | grep smon | grep -v grep |awk -F_ '{print \$3}')\n" EOF echo "$HOST Complete" done ...


0

this seems over complicated. my proposal : for i in $(cat db_hostlist2.txt | grep -v '^#') do echo "Checking for $i" ssh admin@$i '. ./.bashrc; echo "Hostname : "hostname ; echo -e "Total DB is : " ; ps -ef | grep -c [s]mon ; echo " DB are : " \ ps -ef | grep [s]mon |awk -F_ \'{print $3}\' ' ...


2

When the shell parses a command line, it removes quotes but remembers the text structure that they imply.  (That is a gross oversimplification; see bash(1) or Shell Command Language for more details.)  For example, the command-line input -blah apple -secondfruit "green banana" -some more causes the shell to identify six words, or tokens: -blah apple ...


1

When you refer to a variable without quotes around it (e.g. echo $files), the shell splits the value apart on whitespace and passes each term as a separate command-line option. Newlines are treated the same as any other whitespace. The echo program doesn't see the newline characters at all; it just gets an array of strings, each of which is a single ...


1

When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape). Keeping $ as a special character within double quotes permits referencing a quoted variable ("$variable"), that is, replacing the variable ...


2

You have newlines because ls puts them on separate lines. The newlines disappear without the quotes because the shell (bash) passes each unquoted space separated text to the command as a separate argument. Note: The command substitution is done by the shell, not by ls, so you do not need ls. Therefore you can do #!/bin/bash echo *.fastq or ...


1

You were almost there with your first attempt. The problem is that adb unhelpfully adds a carriage return at the end of every line. You can't see it in the basic usage where the output is printed to the terminal, because a carriage return at the end of a line has no visual effect (the carriage return moves the cursor to the beginning of the current line, but ...


1

It is true that your alias problem was a quoting issue, resolved correctly by the user choroba. But you may consider a simpler function: pythonhex(){ printf "\134x%s" "$@"; echo; } Used as: $ pythonhex 5f 74 34 0c c9 7b 9f f8 7a 7c 46 ff ff 5c 31 26 \x5f\x74\x34\x0c\xc9\x7b\x9f\xf8\x7a\x7c\x46\xff\xff\x5c\x31\x26


3

This is a quoting issue. Use single quotes to prevent $0 from expansion, and properly escape each literal single quote: alias pythonhex='sed '\''s/ */\\x/g'\'' | awk '\''{print "\\x"$0}'\'


0

Based on Glenn's input, I have: #!/bin/bash if [ ! -d "$2" ]; then mkdir -p $2 fi find $1/ -type f -print0 | while IFS= read -rd "" filename; do v=$((RANDOM % 4)) if (( v == 0 )); then cp "$filename" $2/`uuidgen`.jpg fi done I added uuidgen because $RANDOM didn't supply a large enough number space to eliminate collision. ...


1

Use yaegashi's answer, or escape the $ like $ sudo chroot mychroot /bin/bash -c "MY_VAR=5; echo \${MY_VAR}"


2

Use single quotes: $ sudo chroot mychroot /bin/bash -c 'MY_VAR=5; echo ${MY_VAR}'


0

escape the last quote like so: mkdir '$"dollars"&<>\dogs'\'


1

You can't escape a single quote within single quotes. But you can juxtapose multiple quoted strings and they will be concatenated. So just use single quotes for the part not containing a single quote, then append a single quote escaped with a \, like this: mkdir '$"dollars"&<>/dogs'\'


2

Presumably, that vzctl exec works like eval. That is, it constructs a shell command line from the concatenation of the arguments it receives. So you should pass it a valid shell command line as argument which would mean another level of quoting. vzctl exec VZID ' sed -i '\''s/\/>/address="$IP\/255.255.255.0"\/\>/'\'' "file.xml"' Note that $IP will ...


0

Have you tried to wrap whole command in quotes and espace the internal with backslashes? I mean: vzctl exec VZID 'sed -i \'s/\/>/address=\"$IP\/255.255.255.0\"\/\>/\' \"file.xml\"' I'd check also double quotes if the single don't work, but I think they should.


2

RAND_FILE=$( find pics/ -type f -print0 | shuf -n 1 -z ) # TODO check that RAND_FILE actually got a file, e.g. what # if pics/ dir is empty, what happens? cp "$RAND_FILE" ... Though hard linking the copy would save space if it's on the same filesystem and the duplicate file will not be modified.


2

The way to handle files with spaces is to use the -print0 directive for GNU find, and the -d option for bash's read command. It's also imperitive to quote the "$variable" find pics/ -type f -print0 | while IFS= read -rd "" filename; do v=$((RANDOM % 2)) if (( v == 0 )); then cp "$filename" dups/$RANDOM.jpg fi done The IFS= and -r bits ...


1

You have not written which shell you are interested in. GNU Parallel quotes any string in ash bash csh dash fdsh fish fizsh ksh ksh93 mksh pdksh posh rbash rc rush rzsh sash sh static-sh tcsh yash zsh. They each need the strings quoted slightly differently. For details see the 'sub shell_quote_scalar' in ...


2

Unix filenames may contain any character (besides '/' or '\0') so this is actually a shell question; the list of metacharacters that need escaping depend on the shell, and the specific configuration of the shell. You appear to be using bash; other shells will fail if the ? or * glob expressions are left unquoted: $ mkdir test && cd test $ ls $ touch ...


1

use quotes to stop bash from globbing. var="A*" echo "$var"


4

What you should do is use an array: mv_params=("foo 1" "foo 2") mv "${mv_params[@]}" The array expansion will properly handle array elements with whitespace or special characters.


4

Dealing with whitespace in filenames in shell scripts is often complicated. In this case, given: MV_PARAMS='"foo 1" "foo 2"' When you write: mv $MV_PARAMS You end up with the shell running mv like this: execve("/usr/bin/mv", ["mv", "\"foo", "1\"", "\"foo", "2\""]) That is, with four arguments: "foo 1" "foo 2" You can use the eval command to ...


10

In Bourne-like shells (if we forget about bugs1 in some old implementations of some shells) var=$otherVar is fine. You're assigning to a scalar variable, so there can't be glob+splitting here, so you don't need to write it var="$otherVar" (though the quotes don't harm here). An exception is for: var=$* and: var=$@ You should only use var="$*". The ...



Top 50 recent answers are included