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)

14
votes
4answers
816 views

keep duplicates out of $PATH on source

I have the following code that's source-d by my .shellrc PATH="${PATH}:${HOME}/perl5/bin" PATH="${PATH}:${HOME}/.bin" export PATH but if I make changes to other code and then source this file, my ...
16
votes
9answers
9k views

Tool in UNIX to subtract dates

Is there any tool in Solaris UNIX (so no GNU tool available) to subtract dates? I know that in Linux we have gawk that can subtract one date from another. But in Solaris the maximum we have is nawk ...
13
votes
1answer
7k views

bash multi line command with comments after the continuation character

Consider echo \ # this is a comment foo This gives: $ sh foo.sh # this is a comment foo.sh: line 2: foo: command not found After some searching on the web, I found a solution by DigitalRoss on ...
9
votes
2answers
1k views

What is meant by a shell is in “vi” mode or “emacs” mode?

This question follows directly from the answer to one of my previous questions here. I am newbie to unix world and this site has helped a lot to understand some core concepts. In this case I am ...
7
votes
4answers
7k views

How do I reverse a for loop?

How do I properly do a for loop in reverse order? for f in /var/logs/foo*.log; do bar "$f" done I need a solution that doesn't break for funky characters in the file names.
6
votes
8answers
4k views

Can shell variable include - character?

I am not able to use - in variables in shell. Is there a way to be able to use it, because I have one script which depends on such named variables: $export a-b=c -bash: export: `a-b=c': not a valid ...
6
votes
3answers
5k views

What is IFS in context of for looping?

I'm learning bash scripting right now. I was reading this thread: How to loop over the lines of a file? What is IFS? With for and IFS:
21
votes
5answers
855 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 ...
19
votes
1answer
3k views

Difference between environment variables and exported environment variables in bash

Bash seems to differentiate between variables which have been exported and those which have not. example: $ FOO=BAR $ env | grep FOO $ set | grep FOO FOO=BAR set sees the variable but env does ...
9
votes
2answers
4k views

What is the significance of single and double quotes in environment variables?

I defined some environment variables in my .profile like this: MY_HOME="/home/my_user" but the variable does not seem to evaluate unless I strip off the quotes and re-source the file. I believe ...
9
votes
4answers
4k views

How to comment multi-line commands in shell scripts?

When invoking long, switch-heavy commands, it's a good practice to write them in shell scripts. Is there an easy way to comment lines in such scripts? I've tried the following methods, but neither ...
7
votes
2answers
6k views

How to cut (select) a field from text line counting from the end?

I know how to select a field from a line using the cut command. For instance, given the following data: a,b,c,d,e f,g,h,i,j k,l,m,n,o This command: cut -d, -f2 # returns the second field of the ...
7
votes
2answers
492 views

How can I use $var in a shell brace expansion of a sequence?

I want to use $var in a shell brace expansion with a range, in bash. Simply putting {$var1..$var2} doesn't work, so I went "lateral"... The following works, but it's a bit kludgey. # remove the ...
7
votes
3answers
6k views

How do I change folder timestamps recursively to the newest file?

I was wondering if anyone knows how to change the timestamps of folders recursively based on the latest timestamp found of the files in that folder. So for example: ...
5
votes
3answers
298 views

Correct textual name for <<

I've just come across the << command, used like so: cat > outfile.txt <<EOF Multi-line content that will be written to outfile.txt EOF Now, I've no idea what this is called, but I'd ...
4
votes
1answer
410 views

What's the conf file reading between login and non-login shell?

I found that those are .bash_profile, .bashrc, .bash_login, .profile. What's the reading sequence between them?
3
votes
6answers
382 views

When does a shell get executed during the linux startup process

I do not understand when does a shell, lets say bash, get executed, which program runs bash initially first.
6
votes
2answers
3k views

what is meant by connecting STDOUT and STDIN?

This is a newbie question. I'm reading a book, it says: Every process has at least three communication channels available to it: “standard input” (STDIN), “standard output” (STDOUT), and ...
6
votes
5answers
797 views

Shell programming, avoiding tempfiles

I often write KSH shell scripts that follow the same pattern: (1) retrieve output from one or more command (2) format it using grep|cut|awk|sed and print it to the screen or to a file In order to ...
4
votes
5answers
2k views

How do I perform an action on all files with a specific extension in subfolders in an elegant way?

My current best bet is: for i in $(find . -name *.jpg); do echo $i; done Problem: does not handle spaces in filenames. Note: I would also love a graphical way of doing this, such as the "tree" ...
4
votes
1answer
1k views

changing current working dir with a script

I've read this question and answer, but it doesn't quit fit my problem, even tho it's similar. I'm writing a script (called cpj) that will launch one of my current projects. I have about 5 of them. ...
19
votes
3answers
3k views

ls command: what does the first line mean?

When I do ls -l I get this: calico@A000505:~/Documentos$ ls -l total 2020 -rwxr-xr-x 1 calico calico 8559 2010-11-16 11:12 a.out -rwxrw-rw- 1 smt smt 2050138 2010-10-14 10:40 Java2.pdf ...
12
votes
0answers
354 views

Should I use $* or $@? [duplicate]

Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
10
votes
6answers
4k views

How can I print lines from file backwards (without using “tac”)?

I have a simple question (I think). I want to print lines from a file backwards without using tac command. Is there any other solution to do such thing with bash?
5
votes
4answers
403 views

Shell, Concatenating 2 strings to reference a 3rd variable

I am trying to do the following in my script: #/bin/bash -x var1=www var2=www2 var3=www3 var4=www4 for i in 1 2 3 4; do echo \$var$i done Obviously, this doesn't work as expected and results in ...
4
votes
4answers
238 views

Grep of many keywords over many files: speeding it up

I am currently facing a "performance problem" while using grep. I am trying to locate the occurrences of many (10,000+) keywords in many (think Linux kernel repository size) files. The objective is to ...
2
votes
2answers
2k views

What does the tilde (~) mean at the end of a filename? [duplicate]

What is the file with the ~ at the end of the filename for? $ ls # aliased to add flags -rwxrwxr-x 1 durrantm 2741 May 16 09:28 strip_out_rspec_prep_cmds.sh~* drwxrwxr-x 13 durrantm 4096 May 16 ...
1
vote
4answers
202 views

List all commands that a shell knows

What can I type at my shell (which happens to be bash) that will list all the commands that are recognized? Also, does this differ by shell? Or do all shells just have a "directory" of commands they ...
65
votes
6answers
68k 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 ...
69
votes
6answers
2k 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 ...
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 ...
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.
82
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 ...
33
votes
4answers
64k 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 ...
26
votes
1answer
779 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, ...
23
votes
2answers
25k 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 ...
35
votes
7answers
26k 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 ...
26
votes
4answers
1k 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 ...
19
votes
5answers
5k 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 ...
16
votes
4answers
2k 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 ...
16
votes
2answers
1k 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 ...
16
votes
6answers
13k 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, ...
19
votes
5answers
3k views

Why is echo a shell built in command?

$ which echo echo: shell built-in command. $ which ls /bin/ls $ which cat /bin/cat Why is echo not an independent utility like ls, ps, cat etc? Why is it shell specific? Any good reasons?
15
votes
5answers
33k 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 ...
11
votes
4answers
947 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. ...
5
votes
3answers
237 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
549 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 | ...
16
votes
6answers
419 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 ...
15
votes
2answers
871 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 ...
12
votes
5answers
486 views

How to achieve portability with sed -i (in-place editing)?

I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...