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)

5
votes
2answers
313 views

Why isn't a semicolon accepted after then?

I'm new to shell programming, and noticed that the following is accepted: if [ $table = "Session" ]; then continue; fi And so is the following: if [ $table = "Session" ]; then continue; fi While ...
6
votes
3answers
119 views

generate links of pages with incrementing variable

consider the links http://unix.stackexchange.com/questions/tagged/linux?page=2&sort=newest&pagesize=15 ...
3
votes
2answers
6k views

Scripting `rm` and `rm -i`

I want to create a script that whenever there are more than three files being removed, it invokes rm -i by default. I know it will go in initialization files but can't seem to get it working. So far: ...
6
votes
4answers
13k views

monitoring file changes + process access to files

I would like to see what's happening in my app server folders, which files are changed by process x or which *.war files has been changed (replaced/created) in last x minutes. I am software developer ...
1
vote
2answers
3k views

Shell script - syntax error near unexpected token `['

When I open my terminal I get the following error: bash: /home/ai/.bashrc: line 75: syntax error near unexpected token `[' bash: /home/ai/.bashrc: line 75: `if [ -x /usr/bin/dircolors ]; then' The ...
10
votes
3answers
10k views

Mac OS X: dircolors not found?

I just switched to a Macbook Air. I installed zsh using homebrew, but when I use some of the code that I (originally had) in my .zshrc, I get an error saying that .dircolors was not found. Below is ...
5
votes
1answer
141 views

Replacing stdout with stderr

Is it possible to redirect the output from a command to replace the text sent to stdout with the text from stderr? $ ./script this line is redirected to stdout this line is redirected to stderr $ ...
11
votes
2answers
394 views

What is the name of the shell feature `>(tee copyError.txt >&2)`?

I need to log stdout and stderr to logfiles, but only show the error messages on screen. I can do this with: cp -rpv a/* b 1> copyLog.txt 2> >(tee copyError.txt >&2) Which I found ...
9
votes
2answers
253 views

How to automatically enter sudo's grace period upon CLI login?

What I would like to do is be able to login and sudo commands immediately without entering a password again. It is very redundant to type my password twice in a row when I need to login and run a ...
1
vote
3answers
31k views

SSH to a host and enter password with shell script [duplicate]

I want to create a file like this : sshTOhost : #!bin/bash ssh -D 1500 user@host #enter password (I don't know how) can any one help me?
2
votes
2answers
1k views

Check recursively if directories in a path exist

I want to check recursively if directories in a given path exist; if they do not, I want to create them. So if I have the path /dir1/dir2/dir3/dir4, if want to check first if dir1/ exists, and if not, ...
7
votes
3answers
14k views

Rename files in the local folder adding a prefix or suffix

I have many files in a folder and I want to add either prefix or a suffix (not both) to them. I checked here and found out I can use for filename in *.jpg; do mv "$filename" "prefix_$filename"; done; ...
2
votes
1answer
186 views

How to compute a thumbnail filename from the shell?

I started noticing some time ago in Xfce4 that when I sent some file to the Trash, tumbler (the Xfce4 thumbnailer) would cause very high I/O load for quite some time. Upon investigating the issue, I ...
41
votes
4answers
2k views

What exactly is an environment variable?

I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but ...
2
votes
1answer
3k views

bash: export: `--wait': not a valid identifier

When I run git-sh from my terminal it works fine. But I get the following warning. bash: export: `--wait': not a valid identifier What is this and how do I get rid of it?
3
votes
2answers
99 views

Is there a way to condense ../.. parts of a path out programatically?

Say for example I have these types of paths to files in a script: /path/to/some/file/../../file1.txt Is there a command I can call such as readlink that will convert this path to its actual ...
5
votes
2answers
2k views

“nohup sudo …” does not prompt for passwd and does nothing

When I run nohup sudo myscript.sh & I only get something like [1] 1220 in return and do not get prompted for a password. The process does not start either. I want to run this script in the ...
4
votes
3answers
3k views

Summing up an array inside of awk?

I have the following piece of code: sum1= sum2= declare -a a echo $temp | awk '{split($0,a,","); name=a[1] ; for(i=2;i<=4;i++) sum1+=a[i] ; for(i=5;i<=7;i++) sum2+=a[i] }' This code is ...
0
votes
2answers
124 views

Monitor servers

I want to monitor three server pages. These three pages contain a simple text (for example it says it's "working fine"). I want to be notified when anything other than this message is displayed, e.g. ...
3
votes
2answers
203 views

Shell-independent way to cp dotfiles from a folder to another [duplicate]

I have two existing directories : foo: directory with dotfiles in it foo2: empty directory I would like to have a solution to copy all dotfiles in foo to foo2. I would like a solution that is not ...
2
votes
1answer
206 views

How to grep including single quote in a file

I have this line from file.php: 'user' => '', What would be the appropriate command using grep to query the exact line?
0
votes
3answers
379 views

Run two commands on same argument

I was wanting to maybe alias a command such that the command ran as per usual but also did something else on the argument/s before it was executed. An example might be: alias ls="echo displaying ...
8
votes
4answers
3k views

How to get mount point of filesystem containing given file

I am looking for a quick way to find the mount point of the file system containing a given FILE. Is there anything simpler or more direct than my solution below? df -h FILE |tail -1 | awk -F% ...
1
vote
2answers
78 views

What happens to argument in alias after using it once? [duplicate]

Why does this only print arg out once? $ alias test="echo $1 $1 $1" $ test arg arg This is on GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu).
7
votes
2answers
1k views

If I type “sudo” at the beginning of a one liner, does it apply to the rest of the commands?

If I type sudo at the beginning of a one liner in bash, does it apply to the rest of the commands? In other words, is this: sudo foo | foo2 | foo3 equivalent to this: sudo foo | sudo foo2 | ...
1
vote
1answer
397 views

Find where a bash command is defined or sourced from? [duplicate]

Sometimes I want to know what a command I type into bash actually evaluates to. Usually I can figure out the location of the executable very easily with which. $ which vim /usr/bin/vim But what if ...
2
votes
1answer
301 views

usage of ! -d in a if condition

When reading the bash script for hadoop management, I found the following one if [ ! -d "${HADOOP_HOME}" ]; then if [ -d "${CDH_HADOOP_HOME}" ]; then what does ! -d "${HADOOP_HOME}" and -d ...
38
votes
13answers
16k views

What's the most resource efficient way to count how many files are in a directory?

CentOS 5.9 I came across an issue the other day where a directory had a lot of files. To count it, I ran ls -l /foo/foo2/ | wc -l Turns out that there were over 1 million files in a single ...
4
votes
1answer
229 views

What does the command “$()” do in UNIX?

What does the command $() do in UNIX? Could someone explain what is the significance of $ in the below examples; $(echo var=10) and eval $(echo var=10)
33
votes
1answer
3k 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 ...
2
votes
4answers
3k views

Wrong root shell in /etc/passwd

I wanted to change the standard shell for my root user to bash but I got the path wrong. i typed /bin/bash instead of /opt/bin/bash. Now my /etc/passwd looks like this: ...
2
votes
1answer
443 views

Screen - inherit current window's environment

In GNU Screen, how can I create a new window whose shell inherits the environment of the shell in the current window? The usual Ctrl+A C doesn't seem to do this.
2
votes
1answer
127 views

bash - inherit directory stack in children

If, in my newly created bash, I do: pushd foo pushd bar dirs -l -v I will get something like 0 bar 1 foo 2 old_dir If I then do bash dirs -l -v I get only 0 bar The same thing happens ...
2
votes
2answers
139 views

Discrepancy between the two expansions

If you input the command ls * and echo *, you will notice the remarkable discrepancy between the results. The former recursively includes while the latter totally excludes files in subdirectories. ...
0
votes
1answer
2k views

How to run a shell command in a server?

I am fairly familiar with computers and web stuff but this is a bit out of my comfort zone. I have a shell script (.sh file) I want to run in a Linux server I have one site hosted (the script is a ...
9
votes
2answers
2k views

The difference between \e and ^[

Gilles wrote: character 27 = 033 = 0x1b = ^[ = \e Demizey wrote: ^[ is just a representation of ESCAPE and \e is interpreted as an actual ESCAPE character Then I also found this line from ...
5
votes
2answers
1k views

Launching an external script with source command

I've been learning shell scripting, by reading good tutorials and reading scripts. I was reading this minecraft init script, and these first lines hit me: if [ -L $0 ] then source `readlink -e ...
1
vote
3answers
86 views

Extend one alias in second alias [duplicate]

It is possible to do this: alias tm=cd /opt/tomcat alias tmbin=tm/bin $ tmbin $pwd /opt/tomcat/bin ?? I know that I can do: alias tmbin=tm;cd bin
3
votes
3answers
948 views

Merging two directory without copying the files

Let's say I have as follows : Folder A C1 file a A2 Folder B C1 B2 file b B3 What I want is to merge these two folders, which would give me : Folder C ...
1
vote
2answers
940 views

Shell: Break both if and for loop at once

Shell: Break both if and for loop at once. My script: ip to name resolve code IP=192.168.27.191 hostNameChecker() { if [ `getent hosts $1 | wc -l` -ne 0 ];then HOST_NAME=`hostname` ...
8
votes
12answers
13k views

How do I run the same linux command in more than one tab/shell simultaneously?

Is there any tool/command in Linux that I can use to run a command in more than one tab simultaneously? I want to run the same command: ./myprog argument1 argument2 simultaneously in more than one ...
0
votes
1answer
83 views

Executing a script in .zshrc

I'm trying to use z (link) to make navigating a little easier. The problem is that I have the following in my .zshrc file, but it doesn't seem to work: export PATH=$PATH:/home/x/z alias ...
7
votes
2answers
896 views

How to test if command is alias, function or binary?

I have command foo, how can I know if it's binary, a function or alias?
2
votes
2answers
689 views

Using calibre from the command line

Using calibre from the command line, in debian testing, how can I insert a tag with the syntax being ebook-meta ebook_file --tags rhubarb but for a range of files, but all by the same author ...
1
vote
1answer
294 views

Access time is not being updated when i run ` ls a ` and then ` stat a`

On CentOS Linux desktop let's create a folder named a. mkdir a Then let's access it ls a and then let's get it's statistics stat a File: `a' Size: 4096 Blocks: 8 IO Block: ...
1
vote
2answers
2k views

The command ` ls -ltu ` fails to list folders/files based on last accessed time

First on your linux desktop create 2 folders. a b Now run this in the terminal ls -ltu the result is drwxr-xr-x 2 root root 4096 Aug 30 20:33 b drwxr-xr-x 2 root root 4096 Aug 30 20:33 a ...
-3
votes
1answer
153 views

Chaining && and || in the shell [closed]

Why does this not work as expected? $ false && { echo ok; echo ok; } && { echo notOK; } and this not: $ false && { echo ok; echo ok; } || { echo notOK; } I don't ...
7
votes
2answers
2k views

How can I get the output and test the dig command?

when we type " dig +short www.example.com " command we get something like that: www.example.com. 192.168.1.1 192.168.1.2 192.168.1.3 I want to get the first address and test it if it is what I ...
43
votes
3answers
21k 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 ...
-1
votes
1answer
2k views

How to create a GUI in shell script without any third party libs just like setup command [closed]

How to create a GUI in shell script without any third party libs (like setup command)