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)

76
votes
6answers
83k views

How to split the terminal into more than one “view”?

From vi, if you issue the command :sp, the screen splits into two "views", allowing you to edit more than one file from the same terminal. Along those same lines, is there a way to have multiple ...
71
votes
6answers
3k views

Run a command that is shadowed by an alias

Let's say i have the following alias in bash - alias ls='ls --color=auto' - and i want to call ordinary ls without options. Is the only way to do that is to unalias, do the command and then alias ...
43
votes
4answers
95k views

How do I grep for multiple patterns?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
28
votes
5answers
2k views

When do you use brace expansion?

I understand what brace expansion is, but I don't know how best to use it. When do you use it? Please teach me some convenient and remarkable examples if you have your own tip.
29
votes
12answers
1k views

How to quickly store and access often used commands?

I have a lot of commands I routinely need to execute, often with the slightest variation. Right now I'm storing them all in .bash_history and use CTRL-R to access them, but I wonder if there's a ...
85
votes
3answers
8k views

Why is “echo” so much faster than “touch”?

I'm trying to update the timestamp to the current time on all of the xml files in my directory (recursively). I'm using Mac OSX 10.8.5. On about 300,000 files, the following echo command takes 10 ...
28
votes
1answer
879 views

Avoiding “BASH-isms” in shell scripts

Does there exist a tool similar to Perl::Critic that will inspect your shell scripts and point out flaws, portability issues, uses of non-standard programs without fallbacks, depreciated program uses, ...
26
votes
3answers
37k views

How to suspend and bring a background process to foreground

I have a process originally running in the foreground. I suspended by Ctrl+Z, and then resume its running in the background by bg <jobid>. I wonder how to suspend a process running in the ...
51
votes
4answers
6k views

What is the difference between “&&” and “;” when chaining commands

What is the difference between the following methods of chaining commands? cmd1; cmd2 cmd1 && cmd2
32
votes
2answers
23k views

How do I use pushd and popd commands?

What are the practical uses of both pushd and popd when there is an advantage of using these two commands over cd and cd -? EDIT: I'm looking for some practical examples of uses for both of these ...
27
votes
4answers
3k views

How to go to the previous working directory in terminal?

In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd command? For example, I'm in /opt/soft/bin and I cd into /etc/squid3 and I want ...
24
votes
3answers
8k views

Precedence of the shell logical operators &&, ||

I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything. true || echo aaa && echo ...
11
votes
2answers
3k views

What is the difference between “du -sh *” and “du -sh ./*”?

The title says it all. What's the difference between those 2 commands? NOTE: I'm more interested in the * and ./* part.
28
votes
5answers
3k views

Does /usr/sbin/nologin as a login shell serve a security purpose?

In my /etc/passwd file, I can see that the www-data user used by Apache, as well as all sorts of system users, have either /usr/sbin/nologin or /bin/false as their login shell. For example, here is a ...
19
votes
4answers
16k views

How to copy some, but not all files?

So, you can use the * as a wild card for all files when using cp within context of a directory. Is there a way to copy all files except x file?
21
votes
5answers
6k views

How to make bidirectional pipe between two programs?

Everyone knows how to make unidirectional pipe between two programs (bind stdout of first one and stdin of second one): first | second. But how to make bidirectional pipe, i.e. cross-bind stdin and ...
20
votes
5answers
42k views

Is there a way to see details of all the threads that a process has in Linux?

For Windows, I think Process Explorer shows you all the threads under a process. Is there a similar command line utility for Linux that can show me details about all the threads a particular process ...
17
votes
4answers
3k views

What happens if you edit a script during execution?

I have a general question, which might be a result of misunderstanding of how processes are handled in Linux. For my purposes I am going to define a 'script' as a snippet of bash code saved to a text ...
17
votes
6answers
15k views

Grabbing the extension in a file name

How do I get the file extension from bash? Here's what I tried: filename=`basename $filepath` fileext=${filename##*.} By doing that I can get extension of bz2 from the path /dir/subdir/file.bz2, ...
16
votes
2answers
3k views

Is there an easy way to programmatically extract IP address?

Is there an easy way to programmatically extract IP address, without tedious parsing of ifconfig? I would not mind simple command output processing using sed to do it but not processing multiline ...
8
votes
5answers
5k views

Shell Script mktemp, what's the best method to create temporary named pipe?

I'm aware its best to create temporary files with mktemp, but what about named pipes? I have an idea, I'm sure it would work, but I would really like an expert opinion. I prefer things to be as ...
4
votes
3answers
2k views

Best practice to use $? in bash?

When I read this answer about $? another question comes to mind. Is there any best practice for how to use $? in bash? Let's have a example: We have a linear script and I we would like to know ...
12
votes
4answers
1k views

How to do a continous 'wc -l' with gnu texttools?

I know of course that cat logfile.txt | wc -l 120 will tell me the number of lines in a file. Whereas tail -f logfile.txt will show me the new lines that another program writes to logfile.txt. ...
9
votes
5answers
1k views

Is there a difference between `;` and `&&` and `|`? [duplicate]

When you want to run multiple commands you can use ;, && and | Like this: killall Finder; killall SystemUIServer, cd ~/Desktop/ && rm Caches Or: man grep | man cat for example. But, ...
5
votes
3answers
379 views

How to scroll large datafile while keeping header

Suppose I have some large datafile, which overflow the screen in both vertical and horizontal direction. How can I browse this file, while the header-lines stay on the screen? For the moment, I am ...
24
votes
3answers
784 views

Multicolored Grep

I'm trying to get each grep command to highlight it's results in a different color. I can do it manually with a line like this: ls -l GREP_COLORS='mt=01;32' grep c | GREP_COLORS='mt=01;31' grep o | ...
23
votes
2answers
1k views

What does “${-#*i}” != “$-” mean?

In /etc/profile I see this: for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null 2>&1 ...
18
votes
6answers
546 views

How could I remember how to use redirection?

I know what program > /dev/null 2>&1 does. It redirects the output to /dev/null and 2>&1 means to redirect the error output in the same place where the output is sent. My ...
16
votes
2answers
1k views

What happens when we type a simple command on shell? [duplicate]

I have a simple doubt on execution of a simple command. As per my understanding, when we type a command such as ls on interactive shell, Shell interpretes the command. Shell creates a child process ...
15
votes
4answers
4k views

Why do I have to re-set env vars in tmux when I re-attach?

I mainly work on a mac and ssh/tmux attach to a Linux machine to do my work. I have ssh-agent running on the Linux machine. I have set -g update-environment "SSH_AUTH_SOCK SSH_ASKPASS WINDOWID ...
7
votes
3answers
2k views

Using notify-send with cron

I am using Arch Linux with KDE/Awesome WM. I am trying to get notify-send to work with cron. I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without ...
5
votes
2answers
278 views

Expansion of a shell variable and effect of glob and split on it

This post actually contains two separate questions but I think grouping them together will give some more context. I have gone through this question on quotes around variables but I do not quite ...
12
votes
1answer
790 views

Can I restrict a user to use special programs?

Is it possible to add a user and restrict him to run special programs? For example after this user logs in, he can only open Firefox to use Internet and no other programs can be run by this user. For ...
7
votes
6answers
7k views

What's the best way to count the number of files in a directory? [duplicate]

If parsing the output of ls is dangerous because it can break on some funky characters (spaces, \n, ... ), what's the best way to know the number of files in a directory? I usualy rely on find to ...
6
votes
2answers
3k views

List elements with spaces in zsh

I've been studying zsh scripting for all of 2 hours at this point and I've hit a wall. I want to go through a list of files that may have spaces in them. I'm open to completely different approaches ...
5
votes
2answers
3k views

Manipulate file name piped from find command

I'm relatively new to Bash and am trying to do something that on the surface seemed pretty straightforward - run find over a directory hierarchy to get all of the *.wma files, pipe that output to a ...
5
votes
6answers
2k views

Portable way to find inode number

At first I used stat -c %i file (to help detect the presence of a jail), which seemed to work on any Linux distribution under the sun. On OS X' I had to use ls -i file | cut -d ' ' -f 1. Is there ...
19
votes
4answers
4k views

How can I create an alias for a git [action] command (which includes spaces)?

Most of my my aliases are of this form: alias p='pwd' I want to alias git commit so that it does git commit -v But trying to create an alias with a space gives an error: $ alias 'git commit'='git ...
12
votes
2answers
8k views

/dev/tcp listen instead of nc listen

With a netcat listener like: nc -l <port> < ~/.bashrc I can grab my .bashrc on a new machine (doesn't have nc or LDAP) with: cat < /dev/tcp/<ip>/<port> > ~/.bashrc My ...
10
votes
2answers
972 views

Closing a file descriptor, >&- vs <&-

In the bash tutorial I am reading, it says that if you open a file descriptor for reading, i.e. exec 3< echolist Then you must close it like this, exec 3<&- However, if you open a file ...
9
votes
1answer
247 views

Security Implications of using unsanitized data in Shell Arithmetic evaluation

In a comment to a recent question, Stéphane Chazelas mentions that there are security implications to double parentheses arithmetic such as: x=$((1-$x)) on most shells. My Google skills seem to be ...
9
votes
2answers
2k views

Use HERE file and redirect output of command

I have the following code in a batch script: mpirun -np 6 ./laplace <<END 100 100 100 0.01 100 3 2 1 END | tail -n 1 > output But it isn't working. What I want it to do is to use the HERE ...
8
votes
3answers
1k views

What does “rm is hashed” mean?

I'm going through http://mywiki.wooledge.org/BashGuide/CommandsAndArguments and came across this: $ type rm rm is hashed (/bin/rm) $ type cd cd is a shell builtin Just a little earlier, the guide ...
8
votes
2answers
6k views

How to make exported shell variables permanent?

export LD_PRELOAD=/usr/lib/libtsocks.so It's ok that I can export in this way, but how can I make it permanent? I want LD_PRELOAD to still be changed after a reboot. I'm using Ubuntu and Fedora
7
votes
3answers
577 views

why for loop doesn't raise “argument too long” error?

I found that this would raise the "argument too long" error: ls *.* And this would not raise it: for file in *.* do echo $file done Why?
6
votes
2answers
1k views

How to trigger error using Trap command

I am using Ubuntu 12.04.2. I am trying to use "trap" command to capture abnormal or error in my shell script but I am also trying to manually trigger "Error" exit. I have tried exit 1, but it won't ...
5
votes
2answers
166 views

How can I force bash to deallocate a brace expanded variable without name?

For benchmarking, I ran the command: for i in {1..100000000}; do echo "$i" line >> file done Bash expanded the braces and stored the list 1 2 3 4 5 6 ... 100000000 in memory. I thought ...
5
votes
5answers
10k views

Disable user shell for security reasons

We have several user accounts that we create for automated tasks that require fine-grained permissions, such as file transfer across systems, monitoring, etc. How do we lock down these user accounts ...
5
votes
4answers
2k views

Where do I change my shell?

If I wanted to use zsh, for instance, rather than bash, where would I make this change for the current user?
3
votes
7answers
578 views

Batch renaming of files [duplicate]

I have a directory full of images: DSCN4749.JPG DSCN4750.JPG DSCN4892.JPG ... And I would like a one-liner to rename them to (say). dscn4749.jpg dscn4750.jpg dscn4892.jpg ... How do I do this? ...