All Questions
4,162 questions
586
votes
7
answers
310k
views
What is the difference between the Bash operators [[ vs [ vs ( vs ((?
I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis).
[[ , [ , ( , ((
I have seen people use them on ...
445
votes
13
answers
494k
views
How to remove a single line from history?
I'm working in Mac OSX, so I guess I'm using bash...?
Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?
428
votes
14
answers
747k
views
How can I get the size of a file in a bash script?
How can I get the size of a file in a bash script?
How do I assign this to a bash variable so I can use it later?
377
votes
6
answers
417k
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 ...
353
votes
5
answers
1.4m
views
How can I get the current working directory? [duplicate]
I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd. It just prints pwd how do I get the current working ...
351
votes
4
answers
455k
views
How to get the pid of the last executed command in shell script?
I want to have a shell script like this:
my-app &
echo $my-app-pid
But I do not know how the get the pid of the just executed command.
I know I can just use the jobs -p my-app command to grep ...
338
votes
11
answers
893k
views
What is the "eval" command in bash?
What can you do with the eval command? Why is it useful? Is it some kind of a built-in function in bash? There is no man page for it..
325
votes
25
answers
1.0m
views
Colorizing your terminal and shell environment?
I spend most of my time working in Unix environments and using terminal emulators. I try to use color on the command line, because color makes the output more useful and intuitive.
What options exist ...
320
votes
12
answers
586k
views
How can I test if a variable is empty or contains only spaces?
The following bash syntax verifies if param isn't empty:
[[ ! -z $param ]]
For example:
param=""
[[ ! -z $param ]] && echo "I am not zero"
No output and its fine.
But when param is ...
307
votes
8
answers
240k
views
How to determine where an environment variable came from?
I have a Linux instance that I set up some time ago. When I fire it up and log in as root there are some environment variables that I set up but I can't remember or find where they came from.
I've ...
290
votes
21
answers
471k
views
How to add a newline to the end of a file?
Using version control systems I get annoyed at the noise when the diff says No newline at end of file.
So I was wondering: How to add a newline at the end of a file to get rid of those messages?
282
votes
13
answers
344k
views
Execute a command once per line of piped input?
I want to run a java command once for every match of ls | grep pattern -. In this case, I think I could do find pattern -exec java MyProg '{}' \; but I'm curious about the general case - is there an ...
279
votes
3
answers
56k
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 ...
250
votes
2
answers
463k
views
There are stopped jobs (on bash exit)
I get the message There are stopped jobs. when I try to exit a bash shell sometimes. Here is a reproducible scenario in python 2.x:
ctrl+c is handled by the interpreter as an exception.
ctrl+z 'stops' ...
243
votes
3
answers
171k
views
Are there naming conventions for variables in shell scripts?
Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...