Linked Questions

3
votes
1answer
3k 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 ...
2
votes
2answers
3k views

Why do we double-quote a dollar sign evaluation in Bash? [duplicate]

I know single quotes will not evaluate what's inside, and double quotes will. I often see people quote-quote the dollar sign evaluation. Here are some examples: for i in "${indices[@]}"; do if [ "${a}...
6
votes
2answers
247 views

Do I need to quote command substitutions when assigning their output to a variable? [duplicate]

I tend to quote command substitutions as shown below even when assigning their output to a variable: var="$(command)" Is that actually needed though? When does it break? The accepted answer ...
3
votes
3answers
440 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 ...
1
vote
3answers
436 views

Shell: only double quote on test -n/-z? [duplicate]

In case of test -e for example: VAR="<this is a file path: /path/to/file/or/dir" test -e $VAR && do_something || do_anotherthing Question: Should I use "$VAR" here?, here I don't like ...
1
vote
1answer
658 views

How can bash aliases be configured to handle spaces in directory names? [duplicate]

The alias below works well in zsh when spaces in directory names are \ escaped, but bash doesn't handle them properly. What changes would be required to make them work with bash properly. dum() { ...
1
vote
1answer
499 views

How to output comma separated values using IFS="," in shell [duplicate]

I have a small script, which does not give comma separated output when IFS is used, but IFS is mandatory as i need it to read many other value. The output of the below script is odi_server1 ...
0
votes
1answer
338 views

Bash whois script calling local information with *? [duplicate]

I'm getting a very strange output from a script. Here is the line run in the terminal by itself: root@KaliTestBox:~/Desktop/dns1# echo $(whois 13.66.39.88 |head -n 40 |tail -n 28 |tr '\n' ' ') But ...
3
votes
3answers
173 views

What situations exist where Bash variables should not be double quoted? [duplicate]

I'm aware of several situations where it's unnecessary to use double quotes when expanding a variable in Bash, such as inside [[...]], or when used on the right hand side of a variable assignment. ...
0
votes
1answer
351 views

How to escape echo? [duplicate]

Edit: I realized that this has nothing to do with $() it's just the result of using echo *. So I just need a way to escape with echo. I'm writing a script that checks what users don't have passwords ...
0
votes
0answers
242 views

How can I safely pass an argument containing '{{}}' to a bash script? [duplicate]

The following arguments are valid for docker command: docker ps -f"status=exited" --format 'table {{.Names}}' #output: # jovial_hellman # modest_blackwell So I have created this script docker-ps-...
1
vote
1answer
76 views

Prevent quote expansion in user-made function [duplicate]

I have the following bash function : tg() { git add -A && git commit -m $1 && git push } But it seems not to work : $ tg "create index for users" error: pathspec 'index' did not ...
-5
votes
1answer
48 views

Why is unquoted $PATH containing whitespaces safe in an assignment? [duplicate]

What does the following from https://unix.stackexchange.com/a/154290/674 mean? In general, in shells other than zsh, $PATH outside double quotes breaks when the value contains spaces or other ...
0
votes
1answer
59 views

How to copy a file to a path with a space in it in Bash Script? [duplicate]

Sample script to copy /tmp/template.txt file to any directory as specified in $1. copy_script.sh if [ $# -eq 0 ]; then echo No Argument echo "Usage: $0 <path>" else cp /tmp/...
0
votes
1answer
33 views

Print awk function out to new line everytime [duplicate]

part of my code is as below : output=$(cat databaselog | awk '{print $9,$1,$2,$6}' ) echo $output >> savedfile Output will be something like this , saved in a new file called savedfile name1 ...
1
vote
0answers
48 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 ...
0
votes
1answer
51 views

How do I make two echos output to a text file on separate lines [duplicate]

So I have a shell script math.sh which takes a number as an argument and echos one added to it and one subtracted from it: #!/bin/bash echo "Add: "$(($1 + 1)) echo "Subtract : "$((...
1
vote
0answers
39 views

Should I quote variable when passing to command? [duplicate]

I always do this when passing variable to command mkdir -p "$dir_to_make" are those quotes needed? I tried to google but didn't find much. I always write them, just interested if they are necessary.
0
votes
0answers
37 views

How to get zsh to not recognize the asterisk in apt-get [duplicate]

I am currently on Pop!OS 20.04, and after installing zsh and setting it as my default shell, when installing apps by using apt-get like follows, zsh says no matches found: sudo apt-get install ...
0
votes
0answers
36 views

Directories printed instead of */ [duplicate]

I have a file like this /* Created by Bla bla bla Copyright by XYZ December 20th, 2020 */ "car" = "Carro"; "door" = "Porta"; I would like to read ...
0
votes
1answer
34 views

grep unexpected behavior when [0-9] pattern is given on ubuntu [duplicate]

When I use [0-9] pattern to search for all numbers in a file, whether to use or not use single/double quotes around the pattern produces different outputs. $ cat numbers this line has 3 this line has ...
0
votes
0answers
28 views

Get immediate folder of file using find (issue with spaces) [duplicate]

I'm trying to search my Movies folder to find the immediate folder of the .MKV file I'm searching for. The below works (echo 5) to save the immediate folder as a variable, BUT, it does not work if $...
0
votes
0answers
22 views

Save renamed name as variable [duplicate]

I am using Gammu for receiving SMS. Once received I am sending them as emails. Yet if sender ID of the SMS contains space, my script is unable to process a file. I.e.: If the file name is ...
0
votes
0answers
19 views

Difference between echo $(cal) and echo "$(cal)" [bash] [duplicate]

I'm a linux noob. When I type echo $(cal) in bash it prints calendar without any formatting. But when i type echo "$(cal)" it prints the complete formatted calendar. What is the distinction ...
0
votes
0answers
14 views

`zsh` and `bash` handle command output differently [duplicate]

In zsh: % RESPONSE=$(ping -c 1 raspberrypi4b.local) % echo $RESPONSE | grep "% packet loss" 1 packets transmitted, 1 packets received, 0.0% packet loss But in bash it's different: $ RESPONSE=$(...
0
votes
0answers
11 views

Prevent string splitting in argument to readlink [duplicate]

I am writing a small script to symbolically link configuration files from my dotfiles directory to another one. I do not have control over the file names and unfortunately one of them is causing me a ...
0
votes
0answers
10 views

Directory with spaces in it as a variable [duplicate]

I made this script that watches one folder and copies any file added to that folder to another destination. It works as expected when the directories have no spaces. With spaces "~/Untitled ...
1178
votes
12answers
2.8m views

How to correctly add a path to PATH?

I'm wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it's not clear how to do this. This way: export ...
341
votes
6answers
326k views

Why does my shell script choke on whitespace or other special characters?

Or, an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
242
votes
3answers
35k views

Security implications of forgetting to quote a variable in bash/POSIX shells

If you've been following unix.stackexchange.com for a while, you should hopefully know by now that leaving a variable unquoted in list context (as in echo $var) in Bourne/POSIX shells (zsh being the ...
184
votes
5answers
78k views

$VAR vs ${VAR} and to quote or not to quote

I can write VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}" the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
174
votes
3answers
146k views

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. What is the correct way to quote my variables? And how should I do it if these are ...
106
votes
4answers
43k views

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a ...
56
votes
2answers
16k views

Documentation on LESS_TERMCAP_* variables?

I see these pretty colors in manpages viewed with less when setting a bunch of these variables. What do they mean, where is the documentation? Is this interpreted by less or termcap? screenshot
26
votes
3answers
51k views

Syntax error near unexpected token `fi`

I don't necessarily want the answer but if someone could point me to some literature or examples. I would like to figure it out. When I run the script I receive an error: Syntax error near ...
58
votes
1answer
8k views

What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?

Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used? Do ...
23
votes
5answers
3k 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 ...
12
votes
6answers
2k views

Too many shebang (script declaration) lines --- any way to reduce their amount?

I have a project comprised of about 20 small .sh files. I name these "small" because generally, no file has more than 20 lines of code. I took a modular approach because thus I'm loyal to the Unix ...
9
votes
5answers
30k views

Find string while knowing part of it and return string

I have a string, for example "Icecream123 AirplaneBCD CompanyTL1 ComputerYU1" Let's say I know that my string will contain for sure the substring IceCream but I don't know what follows it. It might ...
5
votes
7answers
19k views

How to compare 2 strings in UNIX shell script? [duplicate]

I have a variable which stores a string, the output of a sed command. I want to execute a set of commands only if this string value matches either of the 2 other strings. I used the below code. #! /...
10
votes
2answers
7k views

Why are bash tests so picky about whitespace?

As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third? #works if [...
8
votes
5answers
7k views

Script to remove spaces and lowercase in file names

I am trying to write a script that will replace spaces with "-" and make all letters lower case for all files in the current directory. for x in 'ls' do if [ ! -f $x ]; then ...
8
votes
3answers
14k views

Which is the best way to check return result?

I've set a function in a shell script that checks if a folder exists, if it does not exist, tries to created it, and if it can not create the folder (for example if the user does not the right ...
14
votes
2answers
6k views

Quoted vs unquoted string expansion

for i in $(xrandr); do echo "$i" ; done for i in "$(xrandr)"; do echo "$i"; done for i in "$(xrandr)"; do echo $i; done I understand why 1 differs from 2. But why does 3 give a different output from ...
12
votes
2answers
38k views

Zsh: export: not valid in this context

When running this script, I run into an error on this line (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If ...
6
votes
2answers
13k views

bash - True if the length of string is (non)zero? [duplicate]

# touch $$ # gzip $$ # gzip --test $$.gz # echo $? 0 # OUT=$(gzip --test $$.gz) # echo $OUT # if [ -z $OUT ] ; then echo $$ ; fi 26521 # if [ -n $OUT ] ; then echo $$ ; fi 26521 # from bash(1) -...
5
votes
1answer
37k views

bash: integer expression expected

I have a file out.csv I have to check if the name inputed by user exists in the file(comma separated) or not. I am taking name using read but while checking for equality I am getting error ...
4
votes
4answers
10k views

Backslash in variable substitution in Bash

I was working on a Bash script to help partition a hard drive correctly, and I came across a strange problem where I had to append a number to a variable. It took me a while to get the outcome right ...
7
votes
3answers
3k views

Quickest way to move a line in a text file before another line in a text file?

I have the line number 55 in a text file which I want to move just before line number 23 in the same text file. I know that you can move lines up by a certain number of lines 1, 2 but is it possible ...
10
votes
4answers
14k views

Difference between quoting variables in shell script "if" statements?

What is the difference between these two Bash if-statements? e.g. if [ "$FOO" = "true" ]; then vs if [ $FOO = "true" ]; then What is the difference? It seems that both statements work the same.

15 30 50 per page
1
2 3 4 5