Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

5
votes
2answers
4k views

Connect to byobu screen session and execute command?

In a script I am building I'm experimenting with how to automate as much as possible. One of the more interesting challenges is to connect to a byobu screen session and execute a command. So I ...
2
votes
1answer
953 views

Bash Conditional Statements

What are the three formats of conditional statements used in bash scripting?
4
votes
6answers
344 views

Quickest way to change dir from /xxxxx/foo/yyyyyy to /xxxxx/bar/yyyyyy

Using bash, what is the easiest way to 'replace' a given part of the current path with something else? If my current path is of the form /xxxxx/foo/yyyyy, how can I jump to the /xxxxx/bar/baz/yyyyy ...
19
votes
4answers
7k views

Command-line completion from command history

So, I've looked at history and at Ctrl+R, but they are not what I thought I knew. Is there a way that I can type in the beginning of a command, and cycle through the matches in my history with some ...
5
votes
3answers
394 views

Execute a file in an application based on the file's extension in bash

After reading this answer about a zsh feature: If a given extension has a suffix alias, you can execute a file with that extention directly, and ZSH will launch the given program and pass ...
5
votes
3answers
797 views

extracting “tag” information from git with a shell script

The vc bundle is a neat little package that extracts information about a git repo for easy insertion into a LaTeX document. It doesn't currently extract information about whether the current commit is ...
3
votes
2answers
5k views

Passing a variable to a bash script when sourcing it in another bash script?

Suppose I have in main.sh: NAME="$HOME" if [ -f "$HOME/install.sh" ] then . "$HOME/install.sh" "$NAME" fi and in install.sh: echo $1 This is supposed to echo /home/user/, but it echoes ...
9
votes
3answers
4k views

How to properly escape exclamation points in bash?

Today, I was caught redhanded while attempting to code golf a password generator for Twitter. import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9)) 90 chars. ...
7
votes
5answers
6k views

sh startup files over ssh

I have some important commands I need to execute before any sh shell starts. This is required for passing SSH commands in the SSH command (ssh host somecommand) and other programs that run commands. ...
16
votes
1answer
3k views

Is it possible to have vim key bindings in terminal?

I'm getting used to vim bindings (like pressing w to go to word, dw to delete a word, and such) and it's modes (insert, normal, visual), and, out of curiosity would like to know: is there some kind of ...
5
votes
4answers
4k views

find-command for certain subdirectories

Let's say I have a directory dir with three subdirectories dir1 .. dir3. And inside I have many files and other subdirectories. I'd like to search for a file inside, say with a *.c ending, but I'd ...
2
votes
2answers
3k views

How do I correct a directory incorrectly copied into itself?

Given the following situation... <path>/mydir1/mydir2 ...where mydir2 should have overwritten mydir1, but was instead placed inside, and both directories actually have the same filename. How ...
11
votes
5answers
7k views

Adding numbers from the result of a grep

I run the following command: grep -o "[0-9] errors" verification_report_3.txt | awk '{print $1}' and I get the following result: 1 4 0 8 I'd like to add each of the numbers up to a running count ...
8
votes
4answers
9k views

Bash script testing if a command has run correctly

I am working on a bash script that I would like to work for several types of VCS. I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
6
votes
5answers
8k views

How to pass the output of one command as the command-line argument to another?

So I have a script that, when I give it two addresses, will search two HTML links: echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 | sed 's/ /%/g' I want to send this to wget and then save ...
21
votes
4answers
7k views

Quoting in ssh $host $FOO and ssh $host “sudo su user -c $FOO” type constructs

I often end up issuing complex commands over ssh; these commands involve piping to awk or perl one-lines, and as a result contain single quotes and $'s. I have neither been able to figure out a hard ...
23
votes
2answers
14k views

An easy bash completion tutorial? [closed]

I want to learn how to write bash completion scripts. Which tutorial would you recommend for a newbie?
5
votes
3answers
8k views

Storing output of command in shell variable

I have an operation using cut that I would like to assign result to a variable var4=echo ztemp.xml |cut -f1 -d '.' I get the error: ztemp.xml is not a command The value of var4 never gets ...
4
votes
4answers
1k views

Copy text from one terminal into another

I am logged into ssh to a university account from my home computer. Then I find a file that I want to copy onto my local system. So I open a new shell terminal and am about to use scp to copy that ...
17
votes
8answers
7k views

Object-oriented shell for *nix

Preface: I love bash and have no intention of starting any sort of argument or holy-war, and hopefully this is not an extremely naive question. This question is somewhat related to this post on ...
7
votes
1answer
561 views

Is gnu coreutils sort broken?

Consider the following input to sort: cat > foo <<EOM D,,5014978 DD,,25 D,I,1972765530 D,Y,4223624 -,Y,71285059 YA,I,2 EOM Now try running sort foo The output is not sorted when trying ...
7
votes
3answers
3k views

How to open multiple files from `find` output?

I know I can open multiple files found in a dir like so: find -name *.foo -xargs <command> {} \; This works, but when trying to open a bunch of textfiles in gedit at the same time, it opens ...
7
votes
6answers
5k views

Extracting nested zip files

I have numerous zip archives, each of which contains a number of zip archives. What is the best way to recursively extract all files contained within this zip archive and its child zip archives, that ...
2
votes
6answers
2k views

truncating file names

I have a folder that has a number of files all of a similar form as: Dropkick Murphys - 01 - Walk Away.mp3 Dropkick Murphys - 02 - Workers Song.mp3 And so forth... I want to convert them all so ...
1
vote
0answers
849 views

Which scripting language is most widespread in system administration tasks? [closed]

I am migrating to Linux and have some experience with the Mac OS X command line/bash. I'd like to automate system admin tasks via scripting, but I am unsure as which language is the most used in the ...
19
votes
2answers
3k views

aliasing cd to pushd - is it a good idea?

Is it a good idea to use the following alias: cd() { pushd $1; } in bash? I think this would be very useful, since I can then use a series of popds instead of just a cd - once. Is there any ...
0
votes
0answers
608 views

bash get file name and extension [duplicate]

Possible Duplicate: bash : get file extension I have file names like below filename.jar filename.tar.gz filename with no extension ...
9
votes
2answers
785 views

Change directory without typing cd?

Is it possible to make bash change directory in command line simply by typing that directory without any commands like cd? For example is it possible instead of writing this: $ cd /tmp I just want ...
18
votes
1answer
3k views

How do I get bash completion for command aliases?

I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias : alias apt-inst='sudo aptitude install' Is there a way to get the completions ...
17
votes
1answer
2k views

Is there a way in bash to redirect output and still have it go to stdout?

Okay. If I wanted to redirect the output of a program to a file, I'd do something like this prog > file If I wanted to redirect both stdout and stderr to that file, then I'd do prog > file ...
3
votes
5answers
1k views

Does $SHELL store the path to the default shell in Linux?

My administrator says that my default shell is set to bash, but $ echo $SHELL /bin/csh $ Is the default shell related to the SHELL variable at all? Is not, what is the variable used for?
9
votes
6answers
8k views

What is your favourite shell script GUI/CLI editor and why?

I'm using vim for editing bash scripts currently, but i really need to set it up and/or install plugins. Plugin suggestions welcome. Either way i'd like to see which editors/IDEs are there for this ...
16
votes
6answers
2k views

put history command onto command line without executing it

I use !n where (n) is the line number for executing a line in the history file I want executed at the command prompt which I find via history|less. But there is a command line history event I wish ...
10
votes
3answers
2k views

remove extra tilespace from a montage (ImageMagick) composite image?

I have a bunch of photos. They're all the same height, and I'm trying to put them together into a composite image using ImageMagick's montage program via Bash. The problem is that by default, the tile ...
4
votes
2answers
304 views

Simple “Variable” Bash File

So I just want to make a simple bash file, that takes a file name as a parameter. In that bash file, I want to take the name, before the extension, and have it as a "variable", to use in other places. ...
18
votes
1answer
2k views

Aliases vs functions vs scripts

This site says, "Shell functions are faster [than aliases]. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred ...
1
vote
4answers
1k views

Debian sed: can't read

I'm trying to run a command to remove require_once from php files from here (code below), but I am getting the error: sed: can't read -: No such file or directory I am in the correct folder; ...
6
votes
2answers
292 views

How is a graphical application started from a bash session connected to that bash session?

When I start a graphical application from a terminal running bash, that application is somehow connected to that bash session. For example, when the applications dumps some text it will appear in the ...
11
votes
1answer
18k views

In bash is if [ -z “$1” ] and if [ “$1” = “” ] the same?

What is the difference between if [ -z "$1" ] and if [ "$1" = "" ] or are they the same? I have found my self writing the second one in some scripts, but then I read here that the -z flag ...
3
votes
2answers
1k views

How to write a script to execute files in multiple directories

How do I write a script to execute the files in multiple directories? The problem is this: I have many directories, and each has a data file to be read and analyzed by a python script (say, a.py). I ...
4
votes
1answer
109 views

What is this construct in bash? mycommand(){dosomething;}

I actually found following one-liner on commandfu.com: cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext"; } I have not come across a similar ...
17
votes
4answers
22k views

Understanding the exclamation mark (!) in bash

I used history | less to get the lines of previous commands and from the numbers on the left hand side I found the line I wanted repeated (eg. 22) and did !22 at the command prompt and it ...
4
votes
2answers
502 views

How does file descriptor re-assigment work?

In a way this question is a extension of another question. In bash you can do pretty neat stuff with file descriptors. Like { command > /dev/null } 2>&1 | grep filter; to grep on stderr ...
2
votes
0answers
309 views

How can I check which shell I am currently using? [duplicate]

Possible Duplicate: How to test what shell I am using in a terminal? An echo $SHELL will only display the value of the SHELL variable which would indicate the default shell on the system. ...
3
votes
4answers
2k views

Applying a regex to stdin

In programing we often see the use of Regular Expressions. One of the most common forms is: newText = text.replace( /regex/, 'replacementString' ) If stdin is text and stdout is newText, what ...
16
votes
7answers
12k views

Using xargs with input from a file

Say I have a file with the following bob john sue Now these directly corrospond to (in this case) URL pattern such as http://example.com/persons/bob.tar, john.tar, sue.tar. I would like to take ...
5
votes
2answers
21k views

./executable: cannot execute binary file

I have a script that works well when I ssh to the server to execute it myself, but has problems when Hudson, a continuous integration server, runs it. I am automating tests on an embedded linux ...
2
votes
1answer
441 views

Is “export” needed when setting HISTIGNORE in bashrc files?

As I understand it, exporting an environment variable makes it available to any processes spawned from the current one. But the only processes that will be interested in the HISTIGNORE variable (and ...
4
votes
2answers
1k views

Expand KDE activities concept to the shell

Sometimes, I use KDE, and one of the things that I like the most in KDE 4 is the activity concept. At work, it is very useful because I often work on several different projects during one day. ...
19
votes
6answers
7k views

How to print only defined variables (shell and/or environment variables) in bash

The bash builtin command set, if invoked without arguments, will print all shell and environment variables, but also all defined functions. This makes the output unusable for humans and difficult to ...