The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …).

learn more… | top users | synonyms (1)

28
votes
1answer
1k views

Does 'rm .*' ever delete the parent directory?

The expression .* is expanded by bash to include the current and parent directories: $ ls -la total 2600 drwxrwxrwx 2 terdon terdon 2162688 Sep 10 16:22 . drwxr-xr-x 142 terdon terdon 491520 Sep ...
17
votes
7answers
2k views

how to glob every hidden file except current and parent directory

I want to glob every hidden file and directory, but not the current (.) and parent directory (..). I am using bash. Observe current behaviour: $ ls -a . .. ...a ...aa ..a ..aa .a .aa .aaa ...
24
votes
6answers
960 views

How to determine where an environment variable came from

I have a linux instance that I set up some time ago. When I fire it up and log in as root there are some environment variables that I set up but I can't remember or find where they came from. I've ...
8
votes
2answers
2k views

Piping for loop output prevents local variable modification

I'm trying to write a simple bash function that takes, as its arguments, a number of files and/or directories. It should: Fully qualify the filenames. Sort them. Remove duplicates. Print all that ...
7
votes
1answer
3k views

How can I make variables “exported” in a bash script stick around?

I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME, using a script. I have have a shell script set up like this: #!/bin/sh export ...
6
votes
3answers
2k views

How to defer variable expansion

I was wanting to initialize some strings at the top of my script with variables that have no yet been set, such as: str1='I went to ${PLACE} and saw ${EVENT}' str2='If you do ${ACTION} you will ...
4
votes
1answer
2k views

Wrapping a command that includes single and double quotes for another command

I recently learned about watch, but am having trouble making it work with relatively sophisticated commands. For example, I would like to ask watch to run the following command on zsh every three ...
3
votes
4answers
5k views

executing a sh script from the cron

I have a test.sh script #!/bin/sh php /home/v/file.php sh /root/x/some.sh when I execute the file as root from command line it works. sh /home/v/test.sh when I set it to crontab -e (is the root ...
3
votes
2answers
2k views

Is there a way to push shell config information when SSHing to a host?

I know how to set the GNOME-terminals (or xterms!) prompt to green/red regarding the last exit code: vi .bashrc export PROMPT_COMMAND='PS1="` if [[ \$? = "0" ]]; then echo "\\[\\033[0;32m\\]"; else ...
76
votes
8answers
76k views

Colorizing your terminal and shell environment?

I spend most of my time working in Unix environments and using terminal emulators. I try to use color on the command-lin, because color makes the output more useful and intuitive. What are some good ...
72
votes
8answers
9k views

Why *not* parse `ls`?

I consistently see answers quoting this link stating definitively "Don't parse ls!" This bothers me for a couple of reasons: It seems the information in that link has been accepted wholesale with ...
60
votes
24answers
9k views

Quick directory navigation in the terminal

I would like to frequently switch between directories that are in totally unrelated paths, for example /Project/Warnest/docs/ and ~/Dropbox/Projects/ds/test/. But I don't want to type cd ...
28
votes
5answers
11k views

How to check if a shell is login/interactive/batch

I think I understand the differences between an interactive, a login and a batch shell. See the following links for more help: What is the difference between a 'Login' and an 'Interactive' bash ...
32
votes
5answers
3k views

What are the fundamental differences between the mainstream *NIX shells?

What are the fundamental differences between the mainstream *NIX shells and what scenarios might prompt you to use one over the other? I understand that some of it probably comes down to user ...
36
votes
6answers
36k views

How do you move all files (including hidden) in a directory to another?

How do I move all files in a directory (including the hidden ones) to another directory? For example, if I have a folder "Foo" with the files ".hidden" and "notHidden" inside, how do I move both ...
30
votes
10answers
12k views

How do I reuse the last output from the command line?

This is a noob question, but I'd like to know how to reuse the last output from the console, ie: pv-3:method Xavier$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" ...
16
votes
3answers
5k views

make my zsh prompt show mode in vi mode

I use bindkey -v (for bash-ers set -o vi I think that works in zsh too) or vi(m) mode. but it bugs me that I don't have any visual cue to tell me whether I'm in insert mode or command mode. Does ...
39
votes
12answers
7k views

best way to search my shell's history

Is there a better way to search my history file for a command than grep? I do have some idea what the command starts as, but I don't know how far back in the history it is. update: was formerly zsh ...
17
votes
8answers
6k 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 ...
24
votes
6answers
2k views

Why can't I redirect a path name output from one command to “cd”?

I am trying to get cd to accept a directory name redirected to it from another command. Neither of these methods work: $ echo $HOME | cd $ echo $HOME | xargs cd This does work: $ cd $(echo $HOME) ...
18
votes
4answers
811 views

Path independent shebangs

I have a script that I want to be able to run in two machines. These two machines get copies of the script from the same git repository. The script needs to run with the right interpreter (e.g. zsh). ...
11
votes
2answers
1k views

What does `:-` mean in a shell script

I saw this in the end of an awesome shell script but I can't understand the login here because I think it's being short-handed for a longer command. spark ${@:-`cat`} This apears at the end of this ...
21
votes
2answers
24k views

How to get the pid of the last executed command in shell script?

I want to have a shell script like this: my-app & echo $my-app-pid But I do not know how the get the pid of the just executed command. I know I can just use the jobs -p my-app command to grep ...
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 ...
11
votes
8answers
384 views

How to access the history on the fly in unix?

for example, if I do a mkdir thisismyfolder912 I remember there is some easier way to switch to thisismyfolder912 than having to do a cd thisismyfolder912 What is that way and how does it work? ...
10
votes
5answers
2k views

Why is the terminal case-sensitive?

When I do - CD .. instead of cd .. it gives me error saying - CD: command not found Why is the terminal case sensitive when it comes to linux commands? I mean you should be able to execute the ...
9
votes
7answers
1k views

determine shell in script during runtime

As per my knowledge, to determine the current shell we use echo $0 in the shell. Rather I want my script to check in which shell it is running. So, I tried to print $0 in the script and it returns the ...
9
votes
3answers
2k views

Can I make `cut` change a file in place?

The man page doesn't give me much hope, but I'm hoping it's an undocumented (and/or GNU-specific) feature.
7
votes
1answer
5k views

How to check if pipe is empty

I have piped line in bash script and want to check if pipe has data, before feeding program Searching I found about test -t 0 but it doesn't work here. Always returns false. So how to be sure that ...
19
votes
12answers
3k views

Remove duplicate $PATH entries with awk command

I am trying to write a bash shell function that will allow me to remove duplicate copies of directories from my path environment. I was told that it is possible to achieve this with a one line ...
13
votes
2answers
831 views

running script with “. ” and with “source ”

I was wondering if the following two ways of running a bash script are equivalent? . ./myScript.sh source myScript.sh Are they both running the content of the script instead of running the script, ...
7
votes
5answers
544 views

IO redirection and the head command

I was trying to quickly edit an .hgignore file from the Cygwin bash shell today, and I added a line that was a mistake. I'm not sure if this was the best way to do it, but I quickly thought of using ...
2
votes
1answer
778 views

Cut + Copy + Paste in Linux Puppy Wary

Trying to cut + copy + paste from the standard urxvt shell in Puppy Linux Wary 5.3. If I follow this post on http://crunchbang.org/forums/viewtopic.php?id=15085 and I press: in urxvt: ctrl + ...
6
votes
1answer
1k views

Best way to remove file extension from a string?

So, I'm using a script I've made to convert videos to the webm format. A certain program calls the script, sending %f which is the full, absolute file name of the video, like this: converter.sh %f ...
3
votes
1answer
394 views

Difference between ' and " on command line (bash)?

I used to use '' and "" interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a little bit and found that "" ...
7
votes
4answers
546 views

How can I find a file whose name includes a given string, such as “abcde”?

Within a set of directories, how do I find a file whose name includes a given string, such as "abcde"?
7
votes
5answers
5k 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. ...
4
votes
2answers
800 views

Can I read and write to the same file in Linux without overwriting it? [duplicate]

Possible Duplicate: How can I make iconv replace the input file with the converted output? I frequently connect to amazon ec2 using their public DNS names ...
70
votes
4answers
1k views

Can I somehow add a “&& prog2” to an already running prog1?

Most shells provide functions like && and ; to chain the execution of commands in certain ways. But what if a command is already running, can I still somehow add another command to be executed ...
25
votes
1answer
462 views

A shell tool to “tablify” input data

A long time ago I remember using a command that makes its input into a nicely formatted table. For example, for this input, apple 1 100 orange 20 19 pineapple 1000 87 avocado 4 30 The output will ...
37
votes
7answers
28k views

What's the difference between /sbin/nologin and /bin/false

Technically, unless pam is set up to check your shell with pam_shells neither of these can actually prevent your login, if you're not on the shell. On my system they are even different sizes, so I ...
30
votes
5answers
34k views

How to monitor CPU/memory usage of a single process?

I would like to monitor one process's memory / cpu usage in real time. Similar to top but targeted at only one process, preferably with a history graph of some sort.
17
votes
3answers
829 views

What are the responsibilities of each Pseudo-Terminal (PTY) component (software, master side, slave side)?

I am trying to figure out how a tty works1 (the work-flow and responsibilities of each elements). I have read several interesting articles about it, but there are still some blurry areas. This is ...
32
votes
4answers
16k views

How can I send stdout to multiple commands?

There are some commands which filter or act on input, and then pass it along as output, I think usually to stdout - but some commands will just take the stdin and do whatever they do with it, and ...
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 ...
27
votes
2answers
7k views

What does “3>&1 1>&2 2>&3” do in a script?

I saw this line in a script: DEVICE=`dialog --inputbox "Festplatten-Laufzeit auslesen. Gebe Sie das gewünschte Device an: " 0 70 "" 3>&1 1>&2 2>&3` What is 3>&1 ...
18
votes
6answers
7k views

How to check if bash can print colors

I want to know if there's any way to check if my program can output terminal output using colors or not. Running commands like less and looking at the output from a program that outputs using ...
15
votes
3answers
21k views

What does ` (backquote/backtick) mean in bash?

I came across the following command sudo chown `id -u` /somedir And I wonder what is the meaning of the ` symbol. I noticed for instance that while the command above works well the one below does ...
12
votes
3answers
2k views

How to delete part of a path in an interactive shell?

Is there a shortcut in bash and zsh to delete one component of a path? For example, if I type ls ~/local/color/, and the cursor is at the end of line, is there a shortcut to delete the color/ at the ...
33
votes
2answers
1k views

Why is '.' a hard link in Unix?

I've seen many explanations for why the link count for an empty directory in Unix based OSes is 2 instead of 1. They all say that it's because of the '.' directory, which every directory has pointing ...