Questions tagged [bash]

Bash is a free shell for Unix-like operating systems from the GNU Project.

A shell is a command line environment that allows advanced scripting. It is provided on many (all?) operating systems based on or inspired by Unix. Notable OS's are Mac OS X, BSD, Solaris and the different flavours of Linux.

Bash is such a shell created in 1987 to fix some of the problems of the older Bourne Shell. The Bash command syntax is a superset of the Bourne shell command syntax and almost all Bourne shell scripts can be executed by Bash without modification. Bash stands for Bourne Again Shell.

Website: http://www.gnu.org/software/bash/bash.html

11716 questions
455
votes
11 answers

How do I add text to the beginning of a file in Bash?

Hi I want to prepend text to a file. For example I want to add tasks to the beginning of a todo.txt file. I am aware of echo 'task goes here' >> todo.txt but that adds the line to the end of the file (not what I want).
user479534
  • 4,761
381
votes
5 answers

Bash: Iterating over lines in a variable

How does one properly iterate over lines in bash either in a variable, or from the output of a command? Simply setting the IFS variable to a new line works for the output of a command but not when processing a variable that contains new lines. For…
222
votes
26 answers

Bash scripting: test for empty directory

I want to test if a directory doesn't contain any files. If so, I will skip some processing. I tried the following: if [ ./* == "./*" ]; then echo "No new file" exit 1 fi That gives the following error: line 1: [: too many arguments Is…
Anthony Kong
  • 5,028
202
votes
8 answers

Execute a command from another directory in bash

Say that I'm doing this: cd subdir git init cd ../ Is there a way to do this with a single command, or perhaps two, rather than having to move in and out of a directory in order to run a command there? (Not looking for a git-specific solution;…
111
votes
3 answers

How do I get the output and exit value of a subshell when using "bash -e"?

Consider the following code outer-scope.sh #!/bin/bash set -e source inner-scope.sh echo $(inner) echo "I thought I would've died :(" inner-scope.sh #!/bin/bash function inner() { echo "winner"; return 1; } I'm trying to get outer-scope.sh to exit…
jabalsad
  • 1,477
108
votes
2 answers

How to rehash executables in $PATH with bash

The subject says it all, how to rehash the available executables available within one of the $PATHs after having changed things: e.g. removed a binary from one $PATH which is available in another $PATH, changed the $PATH-variable.
Patrick B.
  • 2,978
  • 3
  • 18
  • 18
94
votes
7 answers

How to keep only every nth line of a file

I've got a rather sizable CSV file (75MB). I'm just trying to produce a graph of it, so I really don't need all of the data. Rewording: I'd like to delete n lines, then keep one line, then delete n lines, and so on. So if the file looked like…
Computerish
  • 1,043
93
votes
4 answers

How do I enter a literal tab character in a bash shell?

I wanted to use the sort utility with the -t option to specify tab separators, but sort -t "\t" doesn't work.
Mark
  • 1,063
80
votes
7 answers

How to verify that file2 is newer than file1 in bash?

How can I verify that file2 was last modified after file1? In this example, perl was modified more recently than stack. Is there a bash or Linux command that can compare these files based on the modification time? -rw-r--r-- 1 root root …
lidia
  • 919
  • 1
  • 9
  • 9
76
votes
6 answers

How do I use a Bash variable (string) containing quotes in a command?

Failing, simplified example: FLAGS='--archive --exclude="foo bar.txt"' rsync $FLAGS dir1 dir2 I need to include the quotes as if the command was like this: rsync --archive --exclude="foo bar.txt" dir1 dir2
72
votes
7 answers

How to disable set -e for an individual command?

The set -e command makes a bash script fail immediately when any command returns an non-zero exit code. Is there an easy and elegant way to disable this behaviour for an individual command within a script? At which places is this functionality…
Gustave
  • 1,547
56
votes
4 answers

How to call bash functions

Maybe I am looking at this the wrong way.. But here I what I am trying to do. Do most of my work with Java but just switched to a unix (bash) environment. I am doing this: [~/Desktop/bashPlay]% cat myFunc #!/bin/bash ls2(){ echo "Hello…
55
votes
3 answers

What does echo $((2#$1)) exactly does?

The following bash script displays a decimal number when given binary number. echo $((2#$1)) Why exactly ? I understand that $1 is the input. Maybe 2 is the base (binary). But I can't understand the syntax used.
NanoPish
  • 858
50
votes
5 answers

Use "cd -" without any output

How do I run the cd - command without echoing out any output? I tried cd - 2>&1 /dev/null but that prints out the destination directory. The reason for this is that I would like to use it an a bash function and I would prefer to not have the…
Batandwa
  • 671
49
votes
1 answer

: colon command for bash

Speaking as a bash newbie I have been upgrading my .bashrc via copy/paste + github and I have come across the : command that stumps both me and google. e.g. : ${USER_BASH_COMPLETION_DIR:=~/.bash_completion.d}. Without this statement originally in my…
sh54
  • 647
1
2 3
35 36