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)

57
votes
5answers
21k views

Prepending a timestamp to each line of output from a command

I wish to prepend a timestamp to each line of output from a command. For example: foo bar baz would become [2011-12-13 12:20:38] foo [2011-12-13 12:21:32] bar [2011-12-13 12:22:20] baz ...where ...
4
votes
3answers
17k views

Shell Script - syntax error near unexpected token `else'

With the following shell script, why I am getting errors syntax error near unexpected token `else' Shell Script echo "please enter username" read user_name echo "please enter password" read -s ...
0
votes
2answers
22 views

Using “find” non-recursively?

Using /bin/find /root -name '*.csv' returns: /root/small_devices.csv /root/locating/located_201606291341.csv /root/locating/located_201606301411.csv /root/locating/g_cache.csv /root/locating/...
176
votes
6answers
382k 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 ...
3
votes
2answers
44 views

Can I `join` against a command?

I find join pretty useful. It lets you join file1 against file2 on key fields. Is it possible to do so dynamically against the results of a command, like: join -1 1 -2 1 file1 'curl http://example....
2
votes
2answers
34 views

Default assignment to a null variable with command substitution

I'm trying to use the syntax: A=${B:-C} where A is the variable, B is the value I'm trying to assign, C is the default value if B is null. Now I want to replace B with the command nc -l 443, so if ...
0
votes
0answers
26 views

keep data in 4 digit in array or make data in 4 digite with out using printf, in sheel [on hold]

I need to download a lot a stuff from ftp, so I made a loop with array for the name of each file. The thing is the number from the array can not show as 4 digit, which the file name is (like 0030). I ...
2
votes
3answers
35 views

Specifying redirection options through a variable, in an exec command

I want to be able to specify the redirection command/options via a variable (that I might set based on certain conditions, etc.). But when I run this bash script for example : REDIRECT=">>test" ...
-3
votes
0answers
23 views

Need to create shell script to extract logfile and extract timing detail from it and copy it in excel file [on hold]

I need to copy a logfile (containing details of job run on recent date) from different directory to the current directory and from that logfile i need to find some particular job timing detail to ...
3
votes
5answers
191 views

Comparing multiple options in a bash (string)

I'm trying to enable only certain options when using the read command, and to exit the script if a wrong possibility was entered. Tried many possibilities (array, variables, syntax change), but I'm ...
-1
votes
1answer
18 views

xdg-open open Url in browser with & [on hold]

How To open Below Url in browser by using xdg-open. http://w57.myfranconnect.com/squirrelmail/src/read_body.php?mailbox=INBOX&passed_id=683&startMessage=1
1
vote
1answer
33 views

compress all pdf files recursively

i want to compress all my pdf files in an given directory and its sub-directories using ghostscript. I´m stuck using the find command within loops by filename including spaces. Here some example ...
1
vote
3answers
107 views

How complex can a program be written in pure Bash? [on hold]

After some very quick research, it seems Bash is a Turing-complete language. I wonder, why is Bash used almost exclusively to write relatively simple scripts? Since a Bash shell comes with Linux, you ...
21
votes
3answers
37k views

How do I check if a file is a symbolic link to a directory?

I can check, if a file exists and is a symbolic link with -L for file in *; do if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi done and if it is a ...
-5
votes
0answers
28 views

I want to shell scripting can anybody assist me on this ? Any website or any online course? [on hold]

I want to shell scripting can anybody assist me on this ...Any website any online course.
2
votes
2answers
189 views

Renaming using terminal and regexp to remove leading numbers from filenames

I want the following files to be renamed removing the sequence numbers. 01 X.mp3 to X.mp3 02 Add Me In.mp3 to Add Me In.mp3 I am trying with the below rename command. rename -v -n 's/^\d+\s*([a-z]+...
-1
votes
1answer
53 views

How to grep and treat meta character as normal character? [on hold]

I am trying to recursively find some pattern which contains meta character. Pattern that I searching for is template <int N> I have tried : grep -F -lir "template <int N>" * # ...
21
votes
4answers
35k views

Why doesn't my ~/.bash_profile work?

I'm using Linux Mint. My login shell (cat /etc/passwd | grep myUserName) is bash. After I start my graphical desktop environment and run terminal emulator from it, I could see that .bash_profile is ...
4
votes
2answers
52 views

How can I stop vim from editing directories?

Sometimes I accidentally tell vim to edit a directory instead of a file in it vim directory-name # what I typed vim directory-name/blah.txt # what I meant to type Instead of immediately ...
3
votes
2answers
55 views

Echo the list of process not running

I have developed a script, the script refers to a file to start the required processes on that server. The script times out after 180 secs. At the end of the script I would like to display the ...
0
votes
2answers
911 views

Difference between 'echo' and 'echo -e' [closed]

What is the difference between echo and echo -e? Which quotes (" " or ' ')should be used with the echo command? ie: echo "Print statement" or echo 'Print statement'? Also, what are the available ...
4
votes
3answers
872 views

Unicode text doesn't appear correctly in shell

I'm using the terminal in Kali Linux and I try to type Arabic text the characters are shown but separated (appearance not correct) This what my shell shows me ex. لا ا ل ه ا لا ا ل ل ه, which should ...
10
votes
2answers
412 views

Is there a way for shell script to know which program has executed it?

In *nix world, is there a way for shell script to have information about which program has executed it? Example: /path/to/script1 /path/to/script_xyz in this imaginary scenario, script_xyz would ...
5
votes
2answers
60 views

How to set an alias across all tty shells?

I set an alias in shell tt2 but it won't work in other tty shells, any idea how to make a system wide alias ?
2
votes
6answers
137 views

Renaming files from upper case to lower case

I need to elaborate a little bit more. These commands are actually for a BBS FTN tosser. The configuration file for which these commands reside in, only allow specific declarations. Here are a few ...
6
votes
2answers
380 views

Why does changing $PATH affect child shells, but changing $foo not?

$ unset foo $ unset bar $ echo $foo $ echo $bar $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games $ foo=a $ bar=b $ export bar $ echo $foo a $ ...
4
votes
1answer
73 views

Exploit in bash script parameter possible?

I want to pass an e-mail address from a web form to a bash script. I'm using the following regex: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/ Is this sufficient? Or could one embed an exploit?...
7
votes
1answer
159 views

Why does OS X `install` errors on redirected input when the same version of `install` on linux works fine?

I would like to use install command in order to create a new executable file with pre-populated content (e.g. with single pwd command in it). So I've extended this example which creates a new empty ...
7
votes
4answers
671 views

OpenSSH - map aliases in ~/.ssh/config to IP addresses in command line

Is it possible to query ~/.ssh/config in the command line? Let's say I would like to retrieve what IP address does the particular alias point to in a separate program, is it possible?
2
votes
1answer
60 views

Why do my single quotes vanish on substitution with sed

What is the right command for: sudo sed -i '/, false);/adefine( 'WP_MEMORY_LIMIT', '64M' );' /var/www/html/wp-config.php The result is: define( WP_MEMORY_LIMIT, 64M ); instead of the desired: ...
20
votes
6answers
31k views

In linux, how to delete all files EXCEPT the pattern *.txt?

I know how to delete all txt file under current directory by rm *.txt. Does anyone know how to delete all files in current directory EXCEPT txt file?
2
votes
2answers
580 views

How to control buffering of pipes in bash scripts?

I need my script to be executed with stdbuf -i0 -o0 -e0 otherwise user will wonder why there is no output (becuase of pipe buffer). How can I acheive it? #!/usr/bin/stdbuf -i0 -o0 -e0 /bin/bash ...
1
vote
1answer
99 views

tail file from remote server and nslookup on local

I need a script for resolve hosts. For now I run this script on local machine for i in `tail -F access.log | awk '{print $8}' | awk '{gsub("http://|/.*","")}2' | awk '{gsub("http://|:.*","")}1' | ...
1
vote
1answer
33 views

cat into stdin then pipe into program keeps forked shell open, why?

I'm not sure what's going on but I've been trying to understand what is happening with the input and output. So here is my program. #include <stdio.h> #include <stdlib.h> int main(){ ...
28
votes
7answers
4k views

Why does [A-Z] match lowercase letters in bash?

In all shells I am aware of, rm [A-Z]* removes all files that start with an uppercase letter, but with bash this removes all files that start with a letter. As this problem exists on Linux and ...
35
votes
7answers
3k views

Is `~/Documents` a relative or an absolute path?

This is merely just a vocabulary question, but which keeps turning around in my head. It comes from a practice exam from a LPIC preparation book. The correct answer according to the book is that ~/...
5
votes
2answers
405 views

Shell script: if multiple conditions

I want to create a shell script that echos something if 3 directories don't exist. Here is the code I wrote: test.sh: if [ ! -d "/home/unix/POSTagger2" ] || [! -d "/home/unix/POSTagger2/stanford-...
0
votes
1answer
25 views

What is Alternative of initlog (present in RHEL5) but deprecated in RHEL6?

I am using initlog tool in a cron file to write the output of ntpq -p command in syslog.. But initlog command has been deprecated in RHEL6. Now, I want know the alternative of initlog command in ...
80
votes
3answers
9k views

Why is using a shell loop to process text considered bad practice?

Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
1
vote
1answer
39 views

How to set a path variable without root acess? [duplicate]

My college currently uses a very old (like 3 years old) openJDK 1.7 distribution. And being a student, obviously I don't have root privileges. I wanted to use a much later version downloaded a much ...
0
votes
1answer
13 views

How to list SVN repository name with latest revision number

I want to get for each of my SVN repository its latest revision number. My command looks like currently like this: ls | xargs -n 1 svnlook youngest This prints a list of revision numbers. But I'd ...
0
votes
0answers
18 views

Input Return Key In A Sql script

I have a sql script which when run prompts me to input a value, if i don't input any value and press the enter/return key it will select the default value and will pull all the records. I am trying to ...
16
votes
5answers
14k views

Check if pipe is empty and run a command on the data if it isn't

I have piped a line in bash script and want to check if the pipe has data, before feeding it to a program. Searching I found about test -t 0 but it doesn't work here. Always returns false. So how to ...
1
vote
3answers
163 views

Spaces in assignments in shell scripts

What is the difference between a. var=23 b. var =23 c. var= 23 d. var = 23 There is difference in space around the assignment operator. I am new to unix.
2
votes
2answers
36 views

how to check variable existence and compare with the string in busybox?

I want to use something like that if [[ ! -z "$ENV" && $ENV == 'production' ]]; then echo "production"; else echo "dev"; fi but in BusyBox it does not work :( sh: 1: [[: not found It ...
11
votes
3answers
7k views

find with -execdir

When I run find with -execdir I don't get the results I was expecting. For example: mkdir -p a/b/c find . -type d -execdir touch foo \; $ tree a a ├── b │   ├── c │   └── foo └── foo Directory c ...
20
votes
4answers
2k views

What's the difference between eval and exec?

eval and exec are both builtin commands of Bash (1) that execute commands. I also see exec has a few options, but that is the only difference?
-3
votes
2answers
171 views

Find the number of files with sizes

Find the number of files with sizes > 100KB in /, /bin, /usr, /usr/bin and /usr/sbin directories and output them in a two column format with the name of the directory and the number of files.
-10
votes
2answers
221 views

replace the the first line of all .sh files in a folder

Create list of all .sh files in current folder. Check first line of each file in this list and if file starts with #!/bin/sh then replace that with #!/bin/bash. Only modify files that have #!/bin/sh, ...
22
votes
4answers
37k views

Pass shell variable as a /pattern/ to awk

Having the following in one of my shell functions: function _process () { awk -v l="$line" ' BEGIN {p=0} /'"$1"'/ {p=1} END{ if(p) print l >> "outfile.txt" } ' } , so when called as ...