All Questions
Tagged with bash shell-script
6,897 questions
659
votes
4
answers
506k
views
Using "${a:-b}" for variable assignment in scripts
I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation
VARIABLE1="${VARIABLE1:-some_val}"
or some expand ...
598
votes
4
answers
1.8m
views
In a bash script, using the conditional "or" in an "if" statement
This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that ...
463
votes
18
answers
702k
views
How do I change the extension of multiple files?
I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble changing more than one file.
Here's my code:
files=`ls -1 *.txt`
for x in $...
438
votes
16
answers
89k
views
In Bash, when to alias, when to script and when to write a function?
Noone should need 10 years for asking this question, like I did. If I were just starting out with Linux, I'd want to know: When to alias, when to script and when to write a function?
Where aliases are ...
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 ...
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 ...
318
votes
5
answers
405k
views
What is the meaning of IFS=$'\n' in bash scripting?
At the beginning of a bash shell script is the following line:
IFS=$'\n'
What is the meaning behind this collection of symbols?
283
votes
15
answers
442k
views
Passing named arguments to shell scripts
Is there any easy way to pass (receive) named parameters to a shell script?
For example,
my_script -p_out '/some/path' -arg_1 '5'
And inside my_script.sh receive them as:
# I believe this notation ...
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 ...
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 ...
232
votes
6
answers
482k
views
Can I redirect output to a log file and background a process at the same time?
Can I redirect output to a log file and a background process at the same time?
In other words, can I do something like this?
nohup java -jar myProgram.jar 2>&1 > output.log &
Or, is ...
207
votes
5
answers
469k
views
How do I add X days to date and get new date?
I have Linux ( RH 5.3) machine
I need to add/calculate 10 days plus date so then I will get new date (expiration date))
for example
# date
Sun Sep 11 07:59:16 IST 2012
So I need to get
...
194
votes
7
answers
442k
views
How do I delete the first n lines of an ascii file using shell commands?
I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so ...
181
votes
1
answer
80k
views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
176
votes
4
answers
306k
views
How can I execute local script on remote machine and include arguments?
I have written a script that runs fine when executed locally:
./sysMole -time Aug 18 18
The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script.
Now, this script is ...