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)

2
votes
1answer
577 views

Use the terminal's autocomplete path feature for input to a shell script

I want to make a script that: (1) Gets a path giving the user the ability to use tab to autocomplete, then (2) gets a filename from the user, and then (3) creates a file at that path with extension ...
1
vote
1answer
430 views

How to filter an array of strings in ksh

Please advise what's wrong in my ksh code. I want to remove the IP's as defined in "bb" array from "aa" array so the IP's 255.0.0.0 and 255.255.255.0 will be removed from the list in aa array. When ...
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 ...
11
votes
4answers
8k views

Why doesn't “sudo su” in a shell script run the rest of the script as root?

A sample script can be as below: #!/bin/bash sudo su ls /root When using ./test.sh as the normal user, instead run ls as super user and exit, it switches to root; and when I logout, it executes ls ...
5
votes
4answers
454 views

Equivalent of forward/back buttons for unix shell (when navigating directories)?

I'd like to be able to type in terminal commands of forward/back especially if I went from a directory with a super-long name to home (and want to return back to the directory with the super-long ...
1
vote
1answer
202 views

I quite like mercurial .hgignore-style globbing. Is there a Linux shell that supports it?

I quite like mercurial .hgignore-style pattern globbing. Globs are rooted at the current directory; a glob such as *.c will only match files in the current directory ending with .c. The ...
19
votes
3answers
791 views

Show PATH in a human-readable way

I want to show my PATH environment variable in a more human-readable way. $ echo $PATH ...
2
votes
1answer
194 views

Find out where $PATH got corrupted

I'm using OSX Mountain Lion 10.8.2 $echo $PATH prints this: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/myusername/.rvm/bin I've commented out everything in ~/.bash_profile ...
10
votes
1answer
272 views

How do you set up the environment for es?

I've been playing with es the last couple of days, and it (like rc) has the quirk/feature that it only runs its startup script (.esrc) on login. The usual way to run code in each interactive session ...
4
votes
3answers
643 views

Reading a binary file as an array of bytes or 16 or 32 bits using shell script

I am working on ARM-based modem Linux host machine and I need to read a bin file as an 8-,16- or 32- bits array. I basically need to encrypt the file in question and was thinking of just XOR-ing the ...
3
votes
2answers
128 views

Cron job for every minute executing out of order?

I'm setting up some shell scripting to be executed every five minutes, then every minute on our client's system, to poll a log and pull some timing information to be stored and accessed by another ...
1
vote
3answers
501 views

how does built in commands of a shell implemented in Linux? as a function or thread of shell process?

I know that external commands are run in the shell by creating a separate process. But what exactly happens when a built in command is run in a shell. Are they executed as a function or if the shell ...
0
votes
3answers
83 views

Is there a way to switch to “su -” after switching users through “su”?

Maybe the title wasn't worded quite clear. But basically what I want to be able to do is to switch to having used "su -" instead of the traditional hyphenless "su" after already using "su" to switch ...
2
votes
3answers
997 views

Why do 'ssh host echo $PATH' and printing the $PATH after ssh'ing into the machine give different results?

On a particular host, when I ssh into the machine and enter echo $PATH I get /home/wxy/bin64:/home/wxy/bin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin and if I ...
-3
votes
1answer
4k views

“not a valid identifier” when I do “export $PATH”

When I run export $PATH in bash, I get the error not a valid identifier. Why? Here's my ~/.bashrc file # ~/.bashrc: executed by bash(1) for non-login shells. # see ...
4
votes
1answer
627 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 ...
0
votes
0answers
256 views

Easy way to push data into MongoDB from Bash shell script?

I know you can create a Javascript file that the MongoDB shell will execute upon startup. But I'm hoping for something more "lightweight" than that. I am using a Linux program that has a scripting ...
0
votes
2answers
134 views

Why are there empty lines in the output of my command?: find ~/x/y/ | shuf > ~/Desktop/z.txt

As it turns out the reason for this problem was one filename with several newlines. (No idea how that happened.) find ~/x/y/ | shuf > ~/Desktop/z.txt This command works pretty much as expected ...
4
votes
2answers
227 views

cd to directory of a symbolically linked file

Before I write a script, anyone know an easy way to do the following: $ pwd /foo/bar $ ls -l lrwxr-xr-x 1 username admin 48 Apr 17 2012 foo.sh -> /bar/foo.sh $ cd /bar $ ls foo.sh i.e., in ...
8
votes
2answers
2k views

Executing piped commands in parallel

Consider the following scenario. I have two programs A and B. Program A outputs to stdout lines of strings while program B process lines from stdin. The way to use these two programs is of course ...
1
vote
8answers
134 views

remove content of directory in elegant way

I would like to remove all content of the current working directory. How to do it in efficient and elegant way? I have several files, directories and some of them starts with dot, like: touch .abc; ...
7
votes
3answers
8k views

How to specify characters using hexadecimal codes in `grep`?

I am using following command to grep character set range for hexadecimal code 0900 (instead of अ) to 097F (instead of व). How I can use hexadecimal code in place of अ and व? bzcat archive.bz2 | grep ...
4
votes
3answers
4k views

Terminating a bash shell script running in the background

I often use bash shell scripts to run simple commands for many different files. For example, suppose that I have the following bash shell script, called script.sh, that runs the program/command foo ...
9
votes
2answers
738 views

How to manage huge amount of files in shell?

$ ls ./dir_with_huge_amount_of_files/errors/ Suppose a directory is full of pictures with unix timestamps, I mean a lot measured in many GBs or even more. Shell-commands like ls will get ...
-1
votes
1answer
181 views

SED command and filenames with space

I was hacked, for my php files was infected. Example: http://www.nwww.ma.mk/oscommerce/configweb/apexfash-wp-blog.txt Bad Code: <?php ...
4
votes
2answers
439 views

Convert MAC address to Link-local address with bash

How can I convert a Mac address into an ipv6 Link-Local address? you have to add fe80:: at the start and insert ff:fe in the middle furthermore all leading zeros must be stripped
3
votes
2answers
2k views

how to loop through arguments in a bash script

I would like to write a bash script with unknown amount of arguments. How can I walk through these arguments and do something with them? A wrong attempt would look like this: #!/bin/bash for i in ...
8
votes
3answers
1k views

Why $SHELL doesn't change when I run new shell?

$ echo $SHELL /bin/bash $ /bin/ksh93 $ echo $SHELL /bin/bash $ file /bin/ksh93 /bin/ksh93: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for ...
-1
votes
1answer
55 views

Strange combination of symbols UNIX [closed]

I was looking at one korn shell script examples and I saw this strange combination of symbols: %? So my question is what does it mean?
16
votes
6answers
20k views

How do I can get the size of a file in a bash script?

How do I can get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
3
votes
2answers
135 views

Do redirection operators always open file descriptors in parallel?

1. Consider snippet#1: $ cat test.txt > test.txt cat: test.txt: input file is output file It seems that cat makes its input file descriptor point to test.txt and then when it tries to set its ...
1
vote
2answers
95 views

Reduce redundant logfile

I often have longer redundant log files, redundant in the sense that blocks of text repeat with minor changes. The length of the files makes it difficult to spot the actual information. I had the idea ...
1
vote
2answers
946 views

Shell hangs when doing command from ssh

I'm trying to automate a server update process. What this should do is check for git diff files and list them, to later be copied from one place to the other. Here's my script on my local machine: ...
6
votes
4answers
4k views

cp or rsync, is cp really worth it?

I hope this does not count as a question without a real answer, as I can't seem to find a good reason to use cp(1) over rsync(1) in virtually all circumstances. Should one typically favour rsync over ...
2
votes
1answer
2k views

redirection to file issues with nohup and pipe

For the following command: ssh -t esolve@hostname 'sudo nohup bash -c "ls > log 2>&1 &"' I always got error information: nohup: ignoring input and appending output to ...
5
votes
2answers
368 views

Quoted vs unquoted string expansion

for i in $(xrandr); do echo "$i" ; done for i in "$(xrandr)"; do echo "$i"; done for i in "$(xrandr)"; do echo $i; done I understand why 1 differs from 2. But why does 3 give a different output ...
2
votes
1answer
119 views

Is there a text-only equivalent (no curses) of `zenity --question`?

For my mercurial commit hook, I want to run my test cases and be asked whether I really want to commit if any of the tests fail. I'm thinking something like <run-the-tests> || ...
6
votes
7answers
7k views

Bash way to check if a process is already running in the background (and skip re-execution based on that)?

Can I make a bash command line that only runs a certain command if the process is not already running (in the background)? How do I check*, if a command is already running? (so I can add the next ...
2
votes
3answers
309 views

How to use arguments with 'find -exec'? [duplicate]

I'm trying to generate static html files for all php scripts inside dir: find . -name "*.php" -exec php {} > {}.html \; But it just dumps all results to {}.html. How to do it properly? And how ...
2
votes
4answers
108 views

$PATH duplication issues

If I do echo $PATH I get the following: ...
1
vote
2answers
120 views

Debugging bash functions [closed]

So here I have a simple function that I wish to debug. However, I am unable to debug the desired function even with set -o functrace enabled. Before resorting to asking this question, I had managed ...
3
votes
1answer
266 views

How can I convert this bash function to the fish shell

I'm trying out the fish shell 2.0 to replace bash. I'm trying to convert the following bash function to a fish function, but its giving me hell. I have a several similar functions so if someone ...
3
votes
1answer
158 views

Cron job results are different from executing from own shell

I am using Ubuntu 12.0.4 I have a script basically with the following codes: lockfile=/var/tmp/mylock if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then exit_normally ...
3
votes
3answers
742 views

How to make my tmux which starts via crontab @reboot use bash?

I have a crontab which launches tmux-launching-script as follows : -sh-3.00# crontab -l @reboot /root/scripts/tmux_autostart.sh where #!/bin/bash # setup tmux session tmux new -d -s my_session ...
1
vote
1answer
312 views

7z getting an event not found error in command line

I am using 7zip on my centos machine and ziping a file via command line. This is my syntax 7za a -t7z backup.7z public_html\ -mx0 -xr!restricted_area What i'm expecting with this syntax is to ...
5
votes
1answer
999 views

Can't indent heredoc to match nesting's indent

If there's a "First World Problems" for scripting, this would be it. Mainly asking because I feel like I should know how to get around it if I need to. I have the following code in a script I'm ...
9
votes
2answers
5k views

Is there a simple command for outputting tab-delimited columns in bash?

E.g. I have a file (produced with echo -e "var1\tvar2\t\var3\tvar4" > foo) that are output (in bash): $ cat foo case elems meshing nlsys uniform 2350 0.076662 2.78 non-conformal ...
2
votes
4answers
2k views

sh and grep numbers only

I'm facing a problem and trying to find a solution that works in sh. If I could use bash this code would work: ls /a|grep ^[0-9] Unfortunately this is not the case with sh, and yes I need to use ...
0
votes
2answers
882 views

Cron job not running / not successful?

I have a couple of cron jobs set up to maintain a local copy of a remote database. The first one downloads the latest version of the database from the remote machine, which runs every day and is ...
4
votes
3answers
976 views

renaming files without the **rename** command

I have a directory of files, Measurements Panama 2009-03-22 Session1.xml Measurements Panama 2009-03-22 Session2.xml Measurements Panama 2009-03-22 Session3.xml Measurements Panama 2009-03-22 ...