All Questions

Tagged with
Filter by
Sorted by
Tagged with
3
votes
1answer
2k views

Is `echo $TEST` expanding an asterisk in the variable a bug? [duplicate]

Is this a Bash bug? $ mkdir test && cd test && echo "a" > "some.file" test$ echo '*' * test$ TEST=$(echo '*') test$ echo $TEST some.file Why is the second output the resolution of ...
4
votes
2answers
170 views

Use backslash or single-quotes for field separation

I have always been using single-quotes for the field separation like: awk -F';' ... Quite new to me is the way of using a backslash like: awk -F\; ... is there a technical difference for either, or ...
0
votes
2answers
85 views

Run ssh command without quotes

You can execute a command on a remote server using ssh user@host 'the command' but is there a way to execute the command without quotes? (I am on BSD and using OpenSSH)
2
votes
1answer
48 views

Why I have to not quote a string variable in this specific case?

My Ubuntu/Debian-based Linux update POSIX shell script seems to require me not to double quote the string variable with the stored command, which is being executed. As I don't understand this issue, I'...
0
votes
0answers
28 views

Why does single quotes around arguments fail to prevent “syntax error near unexpected token '('”? [duplicate]

I read that surrounding command line arguments with single quotes is necessary when the command line argument is a string that contains parentheses. However using single quotes around the following ...
3
votes
1answer
253 views

dash: why am I getting the error “Syntax error: EOF in backquote substitution” when my script doesn't even contain a backquote?

#!/bin/sh -- for set_trap_sig in HUP INT QUIT ALRM TERM; do trap -- ' trap -- - '"${set_trap_sig:?}"' EXIT || exit "$?" kill -s '"${set_trap_sig:?}"' -- "$$" || exit "$?"' "$...
0
votes
0answers
16 views

How do you escape “-” in a command in the Shell (Zsh)? [duplicate]

I am trying to rename a file named "-a" to "a" using the command line (zsh shell). I have tried the following mv \-a "a" mv '\-a' "a" mv "\-a" "a" but got mv: illegal option -- a usage: mv [-f | -...
45
votes
1answer
3k views

What is the difference between “…”, '…', $'…', and $“…” quotes?

Sometimes I see scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used? Do they behave differently ...
0
votes
3answers
64 views

Which quoting style GNU Bash variable definitions (mostly for paths)? [closed]

Which of the following quoting styles, for GNU Bash variables, is preferred and why? Two double quotes: VAR="/path/$V1/path with space/$V2". Multiple double quotes: VAR=/path/"$V1"/"path with space"/"...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
2
votes
3answers
643 views

Why can I not access a file named `-` when quoting? [duplicate]

I have a file called -. I want to display its contents. One way is to do cat ./- since cat - reads from standard input. However, why are cat "-" and cat '-' also interpreted by the shell as cat -?
0
votes
2answers
90 views

Is the behavior of escaping special characters inside double-quotes in mainstream POSIX-compliant shells at odds with POSIX?

Per the POSIX Shell Command Language Page: \ The <backslash> shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the ...
3
votes
3answers
175 views

POSIX Shell: inside of double-quotes, are there cases where `\` fails to escape `$`, ```, `"`, `\` or `<newline>`?

Per the POSIX Shell Command Language Page: \ The <backslash> shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the ...
0
votes
1answer
44 views

complicated command replace in files using sed [duplicate]

I want to replace the below strings replace: print 'Status Code: {code}'.format(code=r.status_code) with: print('Status Code: {code}'.format(code=r.status_code)) Here is the command I ran which ...
-2
votes
2answers
38 views

Handling whitespace in filepaths

I have this: muh_dir=`cd $(dirname "$BASH_SOURCE") && pwd` and yeah I tested the above (it has backticks) and it doesn't work well with whitespace in the pwd. On the other hand, this is ...
1
vote
3answers
127 views

SSH with Command Doesn't Run as an Alias

I have the following command to remote into a local server and tail -f the latest log file for an application that I have. The command works perfectly fine from the command line - ssh user@hostname ...
3
votes
1answer
674 views

What's the different between with a ~ and without a ~ in a string variable in Bash? [duplicate]

I'm using a dotfile manage software, named dotdrop. Using a config file named .env, contents like: git_folder="~/code/Git". Also use a script to setup this dotfile tool before the first use of it, as ...
0
votes
4answers
2k views

get output of this command from another server via ssh

I'm trying to ssh into a server and get the system uptime using a specific command but it's failing to execute. this would work locally on almost all unix systems startuptime=`uptime | awk '{print $...
0
votes
0answers
27 views

Quoting of SQL excuted via psql via ssh

I want to execute this SQL line: select * from "foo" where name='bar' I call above line "sql_line" in the next statements. Now I want to call this via psql: psql -c sql_line Now I want to call ...
3
votes
2answers
1k views

How to delete or access a file with a backslash \ in its name?

I have some files here that I copied over to this linux box using the python module pysftp: [jm@spartan tf]$ ls -la total 0 drwxrwxrwx. 3 jm jm 69 Aug 14 13:50 . drwxrwxrwt. 9 root root 238 ...
0
votes
2answers
36 views

Bash : Give builded array to function as a list of argument?

I have this issue with borgbackup, but because the reaction is the same, I will use rsync in my example. I want to build an array of arguments by adding a prefix to each, and then give that array to ...
2
votes
2answers
247 views

Index range of array doesn't allow you to iterate over a new line in bash

I'm working on a simple bash script that fixes a duplicate naming issue on a retropie. The script itself grabs any name that is mentioned more than once in a gameslist.xml file, then stores those in ...
1
vote
1answer
47 views

Is arithmetic expansion related to IFS in some way?

From https://unix.stackexchange.com/a/458074/674 arithmetic expansions are also subject to split+glob so should also be quoted (shift "$((OPTIND - 1))") (here not a problem though as you're using ...
1
vote
1answer
48 views

Loop that lists folders with spaces

Approach I have a directory with named folders, randomly, with blank spaces and periods. I created a small loop (in a script) with the intention of renaming these folders. On the basis that these ...
1
vote
1answer
55 views

variable `-name` primary to `find`

I am trying to construct a compound -name primary for the find command from an arbitrary number of strings in an array, of the form \( -name ${a[0]} -or -name ${a[1]} -or -name ${a[2]} ... \). While ...
2
votes
2answers
7k views

How do I grep multiple patterns from a pipe

I want to find three patterns in a list. I tried typing $ pip3 list | grep -ei foo -ei bar -ei baz but the shell throws a broken pipe error and a large Traceback. How do I grep for multiple ...
3
votes
2answers
875 views

Correctly escaping quotation marks

I have the following command: python -c 'import crypt; print(crypt.crypt("$Password", crypt.mksalt(crypt.METHOD_SHA512)))' Where $Password is a shell variable. How do I correctly expand it as a ...
1
vote
0answers
47 views

Is there any point in quoting simple strings? [duplicate]

Is there any reason for doing something like: a="abc" or a='abc' The quotes seem completely superfluous, but then there might be some hideous corner case... I'm interested in Bash and Sh in ...
5
votes
4answers
2k views

shell: Quote string with single quotes rather than backslashes

How can I quote a string with single quotes? Eg, I can do: $ printf "%q\n" 'two words' two\ words $ Is there a way to get a single- (or double-) quoted string as output, ie: $ MAGIC 'two words' '...
25
votes
3answers
13k views

How can we run a command stored in a variable?

$ ls -l /tmp/test/my\ dir/ total 0 I was wondering why the following ways to run the above command fail or succeed? Thanks. $ abc='ls -l "/tmp/test/my dir"' $ $abc ls: cannot access '"/tmp/test/my':...
11
votes
2answers
4k views

Is it dangerous to run echo without quotes?

I've seen couple of similar topics, but they are referring to not quoting variables, which I know could lead to unwanted results. I saw this code and was wondering would if it be possible to inject ...
8
votes
7answers
1k views

how can I quote a variable expansion inside a string to avoid word splitting?

$ myvar="/path to/my directory" $ sudo bash -c "cd $myvar" In such case, how can I quote $myvar to avoid word splitting because of the white spaces in the value of myvar?
4
votes
2answers
2k views

Escape double quotes in variable

I would like to put this command into a file to be run later: ln -s "$xr"/ya.txt ~ I can do that with (1): cat > zu.sh <<eof ln -s "$xr"/ya.txt ~ eof or (2): printf 'ln -s "%s"/ya.txt ~\...
0
votes
2answers
191 views

What does “newline” mean in the bash documentation?

The bash documentation says the following: A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of ...
6
votes
3answers
840 views

bash quote escaping change in 4.4

I am updating bash on our embedded platform from 4.1.9 to the latest (4.4.12), and I am seeing a behaviour change in this simple scenario of passing escaped arguments into a script. Script /tmp/...
1
vote
2answers
3k views

Why is using eval necessary to pass quoted arguments

I have the following example. #!/bin/bash ARGUMENTS="-executors 1 -description \"The Host\" " # call1 # error: parameter Host" is not allowed java -jar swarm-client.jar $ARGUMENTS # call2 # ...
11
votes
3answers
2k views

How do ssh remote command line arguments get parsed

I've seen the questions and answers about needing to double-escape the arguments to remote ssh commands. My question is: Exactly where and when does the second parsing get done? If I run the ...
3
votes
3answers
185 views

Enclosing vs. not enclosing a variable's value in quotes in bash [duplicate]

When I do something like this: x="hi echo hello" or x='hi echo hello' Then x will contain the string hi echo hello. But when I don't use quotes: x=hi echo hello Then x will contain the string ...
2
votes
2answers
875 views

Are the single quotes and the double quotes handled by “bash” or by “echo”?

When you do something like this: echo 'Hello World' Or like this: x=12345 echo "x is: $x" In the first example, does the echo command receive 'Hello World', or does it receive Hello World? And in ...
2
votes
2answers
97 views

shell variable test expansion problem

I have this code: sed \ $( (( $compress == 1 )) && echo -n '-e /^RMTHOST/ s/$/, compress/' ) \ -e "s|\*\*jobname\*\*|$jobname|g" \ -e "s|\*\*hostname\*\*|$hostname|g" \ -e "s|\*\*hostport\*\*|...
0
votes
1answer
1k views

Bash script failing with unknown option due to space in argument [duplicate]

I am trying to run aws create lambda function. It goes as follows - eval $(aws lambda create-function \ --function-name $FUNCTION_NAME \ --runtime $RUNTIME \ --role $ROLE \ --handler $HANDLER \ --...
1
vote
2answers
4k views

How to use the ls ? option in a bash variable?

I want to use the ls ? option in a Bash variable. How do I get the script to do the same thing as ls -lt foobar_??????.log ? $ cat foobar_ls.sh #!/bin/bash FOOBAR_LIST="foobar_??????.log" ls -lt "$...
2
votes
1answer
380 views

Quoting a path that contains spaces, in FreeBSD - why doesn't it always work?

I have a directory on my FreeBSD server whose contents are created by Windows users over Samba. The filenames and paths within it sometimes contain spaces, as is common in the Windows world. I just ...
0
votes
1answer
290 views

Invoking zmv from bash

I have recently discovered this technique for renaming files using zsh: autoload zmv zmv '(*).JPG' '$1.jpg' which I can also write as: autoload zmv; zmv '(*).JPG' '$1.jpg' This works as advertised ...
0
votes
2answers
489 views

Why echo command does not need “-e” option when escaping “$” character with a backslash

This is regarding bash builtin echo. Per Bash documentation the -e option enables interpretation of backslash escapes. If I execute echo "Total Amount \$500", I see the expected output Total ...
43
votes
7answers
3k views

What's a good mnemonic for shell double vs. single quotes?

I don't do enough scripting to remember, without looking up, whether double or single quotes result in a Unix variable being substituted. I definitely understand what is going on. My question is does ...
1
vote
1answer
9k views

passing string containing spaces as command-line argument from within script

I'm using a bash shell on mac. I want to write a shell script 'gac' such that running > gac one two three produces exactly the same effect as running > git add . > git commit -m "one two ...
7
votes
2answers
3k views

How Can I Expand A Tilde ~ As Part Of A Variable?

When I open up a bash prompt and type: $ set -o xtrace $ x='~/someDirectory' + x='~/someDirectory' $ echo $x + echo '~/someDirectory' ~/someDirectory I was hoping that the 5th line above would have ...
0
votes
1answer
435 views

Do I need to double quote a variable?

I am confused on when we double quote shell variables. Specifically I am using the following sed replace command: sed -i.tmp "/$MY_VAR/d" /foo/bar/file.txt But I am not quoting $MY_VAR. Is this ...
2
votes
2answers
89 views

Mixing local and remote variables and proper usage and quotation

The recommended pattern for bash as far as I know is to always quote the shell variables. E.g. "$VAR" instead of $VAR. But how can I achieve the same safety the quotes provide for variables meant to ...