Skip to main content

All Questions

Tagged with
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 ...
Justin Garrison's user avatar
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 ...
Andrew's user avatar
  • 17.6k
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 $...
afbr1201's user avatar
  • 4,959
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 ...
ixtmixilix's user avatar
  • 13.5k
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 ...
Gilles 'SO- stop being evil''s user avatar
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 ...
maihabunash's user avatar
  • 7,221
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?
Abdul Al Hazred's user avatar
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 ...
Amelio Vazquez-Reina's user avatar
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 ...
Stéphane Chazelas's user avatar
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 ...
Garrett Hall's user avatar
  • 5,541
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 ...
djangofan's user avatar
  • 4,257
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 ...
user avatar
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 ...
Paul's user avatar
  • 9,773
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 $...
kjo's user avatar
  • 16.2k
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 ...
AllenD's user avatar
  • 2,497

15 30 50 per page
1
2 3 4 5
460