Linked Questions
15 questions linked to/from Expansion of a shell variable and effect of glob and split on it
282 votes
3 answers
57k 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 ...
86 votes
3 answers
283k views
Why I can't escape spaces on a bash script? [duplicate]
I'm trying to escape the space char for a path in Bash, but neither using a backslash or quotes works. .sh script: ROOT="/home/hogar/Documents/files/" FILE=${ROOT}"bdd.encrypted" DESTINATION="/home/...
30 votes
5 answers
11k views
Why do I need to quote variable for if, but not for echo?
I've read that you need double quotes for expanding variables, e.g. if [ -n "$test" ]; then echo '$test ok'; else echo '$test null'; fi will work as expected, while if [ -n $test ]; then echo '$test ...
15 votes
6 answers
9k views
Why there is such a difference in execution time of echo and cat?
Answering this question caused me to ask another question: I thought the following scripts do the same thing and the second one should be much faster, because the first one uses cat that needs to open ...
6 votes
4 answers
4k views
Why awk does not ignore "space" as delimiter?
I have a problem with my script. Prelude Firstly, I have a list, 100 lines-file like that: 100;TEST ONE 101;TEST TWO ... 200;TEST HUNDRED Each line have 2 arguments. For example, first line's ...
6 votes
2 answers
16k views
Work-Around to storing array values in an environment variable then calling from Bash script
I looked around for an answer to the question "Can I use an array as an environment variable and then call it from a Bash shell script" and came to the conclusion that it was not "officially" ...
5 votes
5 answers
958 views
Automatic transformation of newlines in shell variable assignment
This came out of one of my comments to this question regarding the use of bc in shell scripting. bc puts line breaks in large numbers, e.g.: > num=$(echo 6^6^3 | bc) > echo $num ...
2 votes
3 answers
1k views
What is (exactly) "list context" (and "string context")?
I have seen several times the use of "list context" and "string context". I know and understand the use of such descriptions in perl. They apply to $ and @. However, when used in shell ...
1 vote
2 answers
8k views
Unix shell script, parameters with spaces
I need to make a shell script called chExt.sh. that accepts a file extension and then multiple files. It then needs to change the file extensions or state that the file does not exist. ./chExt.sh ...
1 vote
1 answer
2k views
understanding .sh file terminology and cmake
I am trying to use cmake and OpenGL to learn computer graphics, and the tutorial included a run.sh file that is supposed to compile/build the cpp programs. Here is the code: #!/bin/bash #calls cd ...
1 vote
2 answers
434 views
Please explain the behavior of these parameter expansions using IFS?
I'm trying to figure out how to use the ${parameter%word} expansion with $@ and $*. It started by trying to make a script to combine pdfs using ghostscript, but I ran into some weird behavior with ...
1 vote
3 answers
2k views
Create user with password, check if user exists and add him to a group
I need to write a bash script which will check if it is run as root user ask for the user name check if user exists add new user with password ask for the group name check if this group exists add the ...
1 vote
2 answers
136 views
How to find files with find tool in system path ($PATH)? Or alternatively, How to specify starting-point directory for find as an expression?
For example, I want to find all symlinks that reference to particular binary in all directories that belong to system $PATH. This can be successfully achieved with manual specification of all ...
-4 votes
1 answer
207 views
Expand a wildcard matching single directory/file
This works beautifully in BASH: $ where=/sys/class/backlight/* $ echo $where /sys/class/backlight/intel_backlight However when put in the POSIX script with /bin/sh as an interpreter, this doesn't ...
0 votes
3 answers
48 views
Implementing a command that executes the given command in a certain path and returns to the current path
my code: execInPath() { prev_dir=${PWD##*/} cd $1 shift res=$($@) cd prev_dir echo res } alias path=execInPath $ path ~ ls gives: bash: cd: prev_dir: No such file or directory (and the files in my ...