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

15513 questions
3 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,207
  • 3
  • 19
  • 21
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…
  • 6,651
  • 3
  • 16
  • 17
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…
  • 8,323
  • 12
  • 30
  • 29
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…
  • 14,895
  • 34
  • 72
  • 77
16 answers

In Bash, when to alias, when to script, and when to write a function?

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just starting out with Linux, I'd want to know: When to…
  • 12,960
  • 26
  • 79
  • 110
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…
  • 4,331
  • 4
  • 11
  • 7
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…
  • 4,710
  • 5
  • 22
  • 28
17 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 on changing more than one file. Here's my code: files=`ls -1 *.txt` for x in $files do mv $x "`basename $files…
  • 3,969
  • 5
  • 21
  • 18
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'
  • 766,554
  • 187
  • 1,586
  • 2,083
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 | awk '{ print $NF }' } But it relies on perl-libwww, perl-html-format, and perl-html-tree being…
Eugene Yarmash
  • 13,945
  • 12
  • 43
  • 56
19 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…
  • 25,579
  • 39
  • 121
  • 184
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…
  • 6,703
  • 19
  • 62
  • 80
6 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?
  • 2,975
  • 3
  • 11
  • 7
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?
  • 2,993
  • 3
  • 15
  • 8
3 answers

Most efficient method to empty the contents of a file

I am aware of three methods to delete all entries from a file. They are >filename touch filename1 filename < /dev/null Of these three I abuse >filename the most as that requires the least number of keystrokes. However, I would like to know which…
  • 3,544
  • 5
  • 16
  • 18
2 3
99 100