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
0answers
21 views

When would you use $* instead of $@ in Bourne shells? [duplicate]

In Bourne shell and its offshoots like Bash, I believe the only difference between $* and $@ is that $@ expands in double quotes so that each argument ends up as a single word even if it contained ...
2
votes
2answers
51 views

Alias doesn't seem to read from STDIN

I'm looking for an alias to convert hex from hexdump output into "Python's" hex notation: $ echo "5f 74 34 0c c9 7b 9f f8 7a 7c 46 ff ff 5c 31 26" | sed 's/ */\\x/g' | awk '{print "\\x"$0}' ...
9
votes
4answers
5k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func ...
3
votes
2answers
71 views

Debian Jessie initramfs-tools strange behavior

My current system is: $ lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 8.1 (jessie) Release: 8.1 Codename: jessie with kernel: $ uname -a ...
2
votes
1answer
23 views

Can a process bring itself back to the foreground on completion? (AIX/ksh)

I have a while loop to count to 5 and echo "Done" when finished. I want to be able to execute the script in the background with "&" and continuing issuing commands while it runs. But when it's ...
13
votes
9answers
9k 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 ...
54
votes
7answers
22k views

How to check if a shell is login/interactive/batch

I think I understand the differences between an interactive, a login and a batch shell. See the following links for more help: What is the difference between a 'Login' and an 'Interactive' bash ...
1
vote
2answers
28 views

List all directories whose contents (files) have not been updated or accessed before a certain date?

Would like to easily located folders to archive/trash and an easy way to automate this would be to choose folders I haven't made any use of for years. Since folders themselves can have different ...
5
votes
1answer
272 views

How to create files named 'a to z' - any trick? [duplicate]

I need to create 26 files named a to z in single command. I thought touch command would be sufficient with the regex, but it doesn't expand [a-z] instead it creates a single file with name "[a-z]". $ ...
2
votes
1answer
87 views

Why root bash output is colored but `sudo ls` output is not?

In bash (Ubuntu) I get colored outputs either as root (after sudo su) or as a normal user. I checked (after reading this post) the files .bashrc for normal user and superuser and the options related ...
3
votes
2answers
7k 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
0answers
50 views

Script won't run in cron but runs fine in root shell

My Problem: I have a python script that runs fine when run manually but does not complete when run using cron. My script fails in cron because it contains pexpect commands that require an interactive ...
0
votes
1answer
41 views

Shell script for logrotating

I want to create a shell script for logrotating. following are my requirement. want to archive the log files to a particular directory lets(/usr/local/jboss-5.10..GA/server/archive/log) older than ...
-1
votes
3answers
3k views

bash: adduser: command not found on centOS

I installed centos 5.5 on my vmware 8 recently and i am trying to add a new user on the system. I am unable to add the user unless i use su - option. I believe it has to do something with path not set ...
38
votes
6answers
6k views

Is there a one-liner that allows me to create a directory and move into it at the same time?

I find myself repeating a lot of: mkdir longtitleproject cd longtitleproject Is there a way of doing it in one line without repeating the directory name? I'm on bash here.
-1
votes
0answers
23 views

How to curl string variable in bash [closed]

this is failing. How can I adjust the command? testFileAddress= "https://aaa.com/test.txt" testFile=$(curl $testFileAddress)
0
votes
2answers
29 views

How to pass an environment variable when change rooting?

I don't seem to be able to pass an environment variable to a chroot: $ sudo apt-get install debootstrap dchroot $ sudo debootstrap trusty mychroot $ sudo chroot mychroot /bin/bash -c "MY_VAR=5; echo ...
1
vote
1answer
43 views

error ./c.sh: line 24: [: too many arguments in shell program

I am trying to make a shell script to print the amount of time user was logged into the system but I encountered a too many arguments error. I tried many methods from the internet but none worked. ...
7
votes
2answers
21k views

bash shell - ssh remote script capture output and exit code?

I wish to use shell to invoke a script on a remote server. I would like to capture the output of that script (its logging messages) and the exit code it returns. If I do this: ssh user@server ...
2
votes
1answer
59 views

n-factor SSH authentication for CentOS 7

How can I enable (and require) n-factor authentication for all SSH connections to a CentOS 7 server? I have read about Google Authenticator, which provides two factor authentication. But that is ...
0
votes
0answers
35 views

Subtract a Number from variable which contains a sentence

This is extended question from http://stackoverflow.com/questions/32081168/reading-a-log-file-with-different-sequences-using-shell-script I'm new to shell. I've written a shell script a code with ...
2
votes
3answers
36 views

Who forks the processes of piped commands in the shell?

Suppose this command in the shell : ls|less Now we know that ls and less are run as two different processes. We know that the shell forks and creates a child process which load the ls program using ...
4
votes
1answer
8k views

ctrl c vs. ctrl z with foreground job

Ctrl+Z stops the job whereas Ctrl+C kills the job. Why is that? Wouldn't the other way make more sense? z@z-lap:~$ sleep 100& [1] 4458 z@z-lap:~$ sleep 200& [2] 4459 z@z-lap:~$ jobs [1]- ...
1
vote
2answers
27 views

shell script for chmod file name but only on new files since last run of the script

i have a directory where new files are uploaded and receive numeric filename as they are uploaded. the file name is numbers that go up. i need to change permissions for the file but only on new files ...
3
votes
3answers
180 views

Checking environment variables' value in Makefile

I have a Makefile target, in which I have to check the value of an environment variable. But, I don't get the exact syntax of it. Tried hard, but can't find it. Any help is appreciated. Environment ...
0
votes
3answers
52 views

regex in `/bin/sh` shell

I would like to remove all files ending with a given set of extensions in a particular directory in a /bin/sh shell. With a /bin/bash I can do it with regex like this : rm path/(*.pdf|*.png) but ...
0
votes
1answer
746 views

openSuse, not running the cron job

I have created a cron job in the /etc/cron.d/dispatchingEmail_I the permissions of dispatchingEmail_I are the following: -rw-r--r-- 1 root root 84 Sep 11 19:52 dispatchingEmail_I content of ...
1
vote
2answers
28 views

Automatic Background Processes

I'm running my Raspberry Pi off an SSH server, and want to be able to access it out of home. Sure, all I need is an IP. But with my terrible internet, it's constantly dropping out. My IP is constantly ...
-2
votes
0answers
32 views

df and du differences too large [duplicate]

-bash-3.2$ df -h | grep /mnt/opt /mnt/opt 197G **163G** 32G 84% /mnt/opt -bash-3.2$ du -sh /mnt/opt **74G** /mnt/opt More then 1/2 the differences between df and du. ...
5
votes
2answers
42 views

posix placement of stdin redirection (<in cmd vs cmd <in)

I always write stdin redirection after the command, because for me it's more natural to have first the command and then the redirections (if any): some-command < input-file > output-file For ...
1
vote
2answers
47 views

How to subtract lines, in reverse, only if greater than previous line

I am working on a shell script that will read the lines of a file, in reverse. I need it to subtract the previous line from the latter, but only if the latter is larger. If it's smaller, then I just ...
4
votes
2answers
47 views

Capturing output/redirection of commands chained by &&

I have a series of commands a,b,c which I am chaining together with &&: a && b && c. I want to catch the output of all the commands to both stdout and stderr. a && b ...
33
votes
3answers
3k views

What does it mean to be “sh compatible”?

I've seen the phrase "sh compatible" used usually in reference to shells. I'm not sure if it also applies to the programs that might be run from within shells. What does it mean for a shell or other ...
48
votes
5answers
4k views

Why does no one use the true Bourne shell as /bin/sh?

I've noticed that basically no system I've ever worked with has /bin/sh as a real executable. It's always a symlink to dash, bash in POSIX mode, or something similar. Why? What are the disadvantages ...
4
votes
4answers
144 views

Null and escape characters

I know that a filename in Linux has no restriction whatsoever excpet for two characters '/' and '\0'. I know that '/' is prohibited because it is a directory separator but is there any other reason ? ...
0
votes
4answers
38 views

Clearing the content of files by size

Considering if I list files in a directory: drwx--x--x 43 root wheel 4.0K Aug 18 12:52 .. -rw------- 1 root root 268K Aug 18 04:31 build_locale_databases_log -rw------- 1 root root 5.2M Aug 18 ...
0
votes
1answer
2k views

shell script to print rows if there is a value in column 2

1 2 3 4 3 1 4 2 1 4 3 2 4 2 1 3 How can I print all rows, on which there is a 2 in the second column.
2
votes
0answers
14 views

restrict private apt repository with rssh - why does apt-get try /bin/sh?

I am trying to set up a private repository via ssh. My sources.list.d/ contains the entry deb ssh://user@host:/repo ./ user is a certificate only account on host. apt-get update works fine. ...
0
votes
2answers
25 views

Putting a single quote in a filename [duplicate]

We can a make a directory named $"dollars"&<>\dogs if we surround it with single quotes like the following mkdir '$"dollars"&<>\dogs' However, what if I want to include a ...
1
vote
0answers
18 views

Write shrunken .img file to device file

I have a backup image of a device file /dev/sda3 that I created using: dd if=/dev/sda3 of=~/sda3.img. The image is very large, so I have re-sized it to remove empty space. My question is can I ...
10
votes
2answers
10k views

How to reset a shell environment?

This issue has been bugging me for a while, and although I've thought I've found my answer through EnvWatcher, unfortunately it only works on Bash. And I use zsh. I would like to replicate the things ...
0
votes
1answer
30 views

Run shell scripts with -x for debugging system v boot

I need to debug the userspace part of the boot process and would like to run all the scripts with sh -x by default so I can observe what’s happening during boot. It's an embedded system that's using ...
8
votes
6answers
31k views

How to search for a word in entire content of a directory in linux

need to search for something in entire content I am trying: find . | xargs grep word I get error: xargs: unterminated quote How to achieve this?
19
votes
3answers
7k views

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
1
vote
4answers
87 views

Set, backup and restore colors in the terminal

I use this program to display all color available in the terminal. That waht I get: This program doesn't show me colors below 100. Why? Is there a way to display them. Is the are way to change ...
0
votes
1answer
35 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' | ...
2
votes
2answers
97 views

Find differences in file and sorting file by key

So I have two language files, english and french. They are in this format: key=translation An example in English would be: ui.title=My Title And in French: ui.title=Mon Titre So I need a ...
0
votes
0answers
23 views

Create and Write .properties file

I need to write a shell script to create and write a file with .properties extension, that contains 3 values, where the first 2 values are user-defined and constant, the 3rd value will be the filename ...
3
votes
2answers
56 views

Convert a value into a “Binary Number” in a shell script

I know of bc: $> var="304" $> echo "obase=2; $var" | bc 100110000 Could it be done in shell (no external call)? This question: Binary to hexadecimal and decimal in a shell script Asks how to ...
-1
votes
1answer
27 views

sed throwing sed: -e expression #(number), char (number): unknown command: `(letter)'

I wrote a shell script which reads some files and copy all the data from these files to other consolidated file. This happens multiple times and previously copied data will not be copied again. For ...