Questions tagged [shell-script]

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

Shell scripting is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Further reading

16187 questions
654
votes
10 answers

Why is it better to use "#!/usr/bin/env NAME" instead of "#!/path/to/NAME" as my shebang?

I notice that some scripts which I have acquired from others have the shebang #!/path/to/NAME while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME. Both seem to work properly. In tutorials (on Python, for example), there…
TheGeeko61
  • 6,911
  • 3
  • 17
  • 18
646
votes
4 answers

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 other variables VARIABLE2="${VARIABLE2:-`echo…
Justin Garrison
  • 7,359
  • 4
  • 20
  • 21
529
votes
4 answers

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 a loop control variable fname iterates over the…
Andrew
  • 16,315
  • 34
  • 73
  • 77
529
votes
8 answers

How can I pass a command line argument into a shell script?

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or string into the script. How do I approach doing…
Paul
  • 9,163
  • 12
  • 32
  • 30
443
votes
2 answers

Using 'sed' to find and replace

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an example usage of the 'sed' command. Say I wanted to…
roo
  • 4,591
  • 4
  • 12
  • 7
427
votes
16 answers

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 concerned, I use aliases for very simple…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
420
votes
18 answers

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 $files do mv $x "`basename $files…
afbr1201
  • 4,529
  • 5
  • 22
  • 18
400
votes
4 answers

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between…
detly
  • 5,020
  • 6
  • 23
  • 29
370
votes
6 answers

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 file names). I encountered a problem such as the…
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
361
votes
21 answers

How do I trim leading and trailing whitespace from each line of some output?

I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into? Example file: test space at back test space at front TAB at end TAB at front sequence…
rubo77
  • 27,777
  • 43
  • 130
  • 199
342
votes
27 answers

How can I get my external IP address in a shell script?

I need to find my external IP address from a shell script. At the moment I use this function: myip () { lwp-request -o text checkip.dyndns.org | awk '{ print $NF }' } But it relies on perl-libwww, perl-html-format, and perl-html-tree being…
Eugene Yarmash
  • 14,675
  • 14
  • 50
  • 57
316
votes
12 answers

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 empty except for one (or more) space characters, then the…
maihabunash
  • 6,973
  • 19
  • 64
  • 80
315
votes
7 answers

How create a temporary file in shell script?

While running a script, I want to create a temporary file in /tmp directory. After execution of that script, that will be cleaned by that script. How to do that in shell script?
Bhuvanesh
  • 3,285
  • 3
  • 12
  • 7
294
votes
4 answers

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
  • 25,760
  • 23
  • 64
  • 88
290
votes
2 answers

How to write startup script for Systemd?

I have 2 graphics cards on my laptop. One is IGP and another discrete. I've written a shell script to to turn off the discrete graphics card. How can I convert it to systemd script to run it at start-up?
Sharique
  • 3,113
  • 3
  • 16
  • 8
1
2 3
99 100