Tagged Questions

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)

8
votes
4answers
838 views

What does echo $-1 display?

What does the output of echo $-1, echo $-2, echo $-3.. mean? On one of my Linux boxes, it shows me: echo $-1 imsBEl1 echo $-2 imsBEl2 And on another Linux box, it shows: echo $-1 himBH1 echo $-2 ...
-1
votes
0answers
41 views

How to write a code which would output combination of two other files? [on hold]

I have two files. Using these two files as sources, I need to generate an output file. File1 will contain resistors connected to nets (Syntax: R1 Terminal1 Terminal2): See below R1 7n 1n R2 8n 1n ...
5
votes
2answers
376 views

How to treat strings with spaces as a whole in bash? [duplicate]

I'd like to build an error handler which will make an empty file marked with the error_occur time. The core idea is to use the result of date command as a parameter. I did: time_stamp=$(date) touch ...
23
votes
1answer
1k views

Why can Shell builtins not be run with capital letters but other commands can?

Why is this? When I do this CD ~/Desktop It doesn't take me to the Desktop. But this: echo "foo bar" | GREP bar gives me: bar
4
votes
3answers
201 views

How do I find a file with a name which depends on the current date (and a random component)?

I have to check a particular path every day and find the file with a name of the form: StaticData_Sets_yyyymmdd-232550.txt The date in the name after the string StaticData_Sets_ is updated every ...
0
votes
1answer
43 views

Why does “redirect to a file” is executed as user, even with sudo? [duplicate]

I often encouter this problem and it really bugs me on why it is so. So let's take this example into account : [me@localhost:~]$ sudo command > /some/file.log As far as I know, this will not ...
2
votes
5answers
188 views

Why do newline characters get lost when using command substitution?

I have a text file named links.txt which looks like this link1 link2 link3 I want to loop through this file line by line and perform an operation on every line. I know I can do this using while ...
-5
votes
1answer
57 views

Extract multiple lines from text file [on hold]

Source : ! hostname RTR-1 ! boot-start-marker boot-end-marker ! logging buffered 64000 informational no logging console ! interface FastEthernet0/0 description LAN ip address 172.16.29.250 ...
1
vote
1answer
52 views

Why is the result of this for and while loop different? [duplicate]

I have a for loop: for host in $(cat ./hosts) do echo -e "$host" ssh -o "StrictHostKeyChecking no" $host "uptime" 2>/dev/null echo -e "\n" done and a while loop which is supposed to the ...
20
votes
2answers
2k views

How does `cat <> file` work?

cat < file prints the contents of file to stdout. cat > file reads stdin until Ctrl+D is detected and the input text is written to file. cat <> file, at least in my version of Bash, ...
0
votes
1answer
47 views

grep: input file 'X' is also the output

I use ubuntu 14.4, and attempt to redirect the output of grep command to a file, but I keep getting this error: grep: input file 'X' is also the output I've searched for this issue and just found ...
-1
votes
1answer
17 views

How do I use export setu command [closed]

What is the difference between export and export setu command? I have searched online these command (export setu) but it's not found.
4
votes
1answer
39 views

Why does BASH process substitution not work with some commands?

On occasion process substitution will not work as expected. Here is an example: Input: gcc <(echo 'int main(){return 0;}') Output: /dev/fd/63: file not recognized: Illegal seek collect2: ...
-2
votes
0answers
23 views

Need Script to Find Duplicate PID in a process [closed]

I need a Shell script to find the duplicate pids in the process. Please advise me Thanks, Srikanth.V
1
vote
0answers
15 views

does sudo always set process limits to the numbers from /etc/security/limits.d?

I have observed the following behavior: [ec2-user@ip-10-66-68-55 ~]$ uname -a Linux ip-10-66-68-55 3.4.103-76.114.amzn1.x86_64 #1 SMP Fri Sep 12 00:57:39 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux ...
43
votes
2answers
2k views

Is >&- more efficient than >/dev/null?

Yesterday I read this SO comment which says that in the shell (at least bash) >&- "has the same result as" >/dev/null. That comment actually refers to the ABS guide as the source of its ...
1
vote
2answers
30 views

Will a shell script execute in Solaris? if not, which is the fastest way to make it compatible

I have written a shell script with #!/bin/sh shebang. I've heard that it won't work alongside with Solaris. And that I have to write a korn shell script. But in one of the threads an individual ...
-1
votes
1answer
77 views

How to insert the entry into the newline?

I have the following entry in a tab delimited file test1.txt key1 10|15 20 30 11|xx 21 31 key2 527|240 269 0 0 0 2462 546 281 0 0 Is there any way I can transform this file into the below format? I ...
1
vote
2answers
50 views

How to use sudo to delete the contents of a directory

I have a legitimate need to empty a directory to which my account does not have access (it is a cache directory used by an application, and is owned by the account that runs the application.) My ...
3
votes
2answers
275 views

grep on a variable

Lets say I have a variable line="This is where we select from a table." now I want to grep how many times does select occur in the sentence. grep -ci "select" $line I tried that, but it did not ...
0
votes
0answers
24 views

route -n gives output “route: fscanf”

If I boot my system and no interface has an IP address I get the above output. However if I give an interface an IP, even if I remove it again, this will disappear. Why does this happen? Is it that ...
4
votes
1answer
260 views

Why does “cat /tmp/out1 > /tmp/in2 &” appear as “bash” with the “ps” command?

Consider: mkfifo /tmp/out1 mkfifo /tmp/in2 cat /tmp/out1 > /tmp/in2 & When I run ps it appears as bash in the CMD section. Why?
0
votes
0answers
80 views

How I can modify a sh command? [closed]

I need to modify the code for one of the sh commands. For example: Add a one line of code that prints your name when a command is executed by a user on the shell. I want editing of source code for ...
1
vote
1answer
27 views

How to check PIPESTATUS equivalent during PROMPT_COMMAND execution?

I have a custom $PROMPT_COMMAND in bash that logs the last executed command and its return code. I was using $? for the latter happily until I realized that for piped commands this was insufficient. I ...
0
votes
4answers
40 views

How to save in a variable the convert from decimal to binary without use an echo

I've been trying to save in a variable the value of the convert of a number from decimal to binary, like this: num1=10 echo "obase=2;$num1" | bc | tee -a register.txt but I don't want to show it ...
2
votes
2answers
36 views

saving the output of command line in a text file?

I am using the following command line to analyse data: unpackdcm -scr ${in} -targ ${out} This command is printing on the screen status and description about the progress in the job. In order to ...
1
vote
1answer
17 views

Print difference of column1 if column2 values are same

I have a file something like this: 19:32:19 4599544 19:32:22 4599544 19:33:07 4599545 19:33:11 4599545 19:33:58 4599546 19:34:01 4599546 1st column is ...
0
votes
1answer
35 views

Howto adapt this bash construct to a sh valid construct

On converting some bash scripts to a platform where only /bin/sh is available i'm stumble about this bash construct: #!/bin/sh RSTPD=/sbin/rstpd RSTPCTL=/sbin/rstpctl RSTP=0 ...
0
votes
0answers
27 views

Unix Shell script to delete all traces of itself after execution through crontab

My Question has 2 parts. Help with both parts is greatly appreciated. GIVEN INFO: - Lets assume there are 2 Folders, Folder A and Folder B. - Folder A contains many files. - Folder B contains the ...
0
votes
1answer
25 views

saving contents of echo output to variable in non-bash script

I'm used to doing something like this in my bash scripts to echo output to a variable: myvar=`echo $OSTYPE` echo "my os is of type: ${myvar}" On this current project however I'm using sh, which on ...
-2
votes
1answer
44 views

How do I automate this in a shell script?

I'm using the application PlexConnect on my home server (a headless OSX box) and I have to ssh in (from an iMac) and manually kill the Python process sometimes with the following: ssh 192.168.0.7 ...
2
votes
2answers
46 views

An alias for a command to kill stopped jobs

I wanted to add an alias to my .bashrc so that I could kill all stopped jobs with a command like kill_stopped. I am aware that kill `jobs -p` can be used to accomplish this but I'd rather have an ...
1
vote
1answer
49 views

command not found via shell script but works on terminal

In my .sh file I cannot invoke ant or mv or cp commands but the same commands executes on terminal.below is my script sample.sh file #! /bin/sh cp filename.so filename_org.so android update project ...
2
votes
2answers
173 views

Shell script written in a different shell than what my current shell

If the shell script is #!/bin/sh But the value of my $SHELL is ksh. Will it make a difference if I changed my ksh to sh, and then executed the script. ie. will a script have different behaviour ...
1
vote
1answer
22 views

several virtual profiles for the same user

Many times there are several "identities" which one user uses: for work, open-source, academia etc., all require different ssh keys, editor configurations, git configs etc. I was wondering if there ...
10
votes
4answers
437 views

Regular expression using \\ vs using \

Why does grep e\\.g\\. <<< "this is an e.g. wow" and grep e\.g\. <<< "this is an e.g. wow" do the same thing? If I add a third slash, it also has the same result. BUT, once I ...
2
votes
1answer
86 views

How to pipe a command output to multiple programs

I'm using FFmpeg and I have made a custom application which is a TCP server. I 'm using duplicate output in ffmpeg. What I want is, for each output I want to start my program to listen on a specific ...
6
votes
3answers
781 views

What does “if !” mean?

I came across a if/then statement like this: if ! foo ; then echo "blah" exit 1 fi What specifically does if ! mean? "If the result of foo is not true?" "If the exit code of foo ...
1
vote
3answers
93 views

How to enter foo-* folder without actually using wildcard or shell expansions?

Short story I'm looking for the command to enter first found foo-something directory like: cd foo-* but without using wildcard (or other special shell characters). Long story As part of the ...
2
votes
2answers
44 views

Optional arguments after or before the mandatory arguments?

I'm creating a shell script and I need it to take two mandatory arguments and one optional. How do I check that? In most scripts I've seen, the optional arguments are passed before the mandatory ones, ...
2
votes
2answers
50 views

Environment variable not expanded inside the command line argument

I have a file user-pid.out2 which has "usernumber" and "process id" as two columns. based on usernumber I want to find corresponding process id. the first two lines below does not show the output ...
-1
votes
1answer
53 views

Is it possible to run two commands at the same time in a shell script?

I know that on the command line I can use & to run a command in the background. But I'm wondering if I can do it in a script. I have a script like this: date_stamp=$(date +"%Y-%m-%d" ...
3
votes
2answers
85 views

How to use getopt in bash command line with only long options?

There is a getopt command in bash command line. getopt can used with short options (such as getopt -o axby "$@"), and can be used with both short and long options (such as getopt -o axby -l long-key ...
2
votes
2answers
193 views

Meaning of \ before environment variable

In ksh on an old Solaris box I used: export PS1="$PWD $" to set the prompt to the current directory. It works great until you cd elsewhere, then you see that it's not evaluating PWD each time. I ...
47
votes
2answers
3k views

Shebang starting with `//`?

I'm confused about following script (hello.go). //usr/bin/env go run $0 $@ ; exit package main import "fmt" func main() { fmt.Printf("hello, world\n") } It can execute. (on MacOS X 10.9.5) $ ...
5
votes
3answers
700 views

Why does “$ sudo echo 'yo' ” work even though echo is a shell built in function? [duplicate]

I understand that sudo cd /directory will return: sudo: cd: command not found because cd is a shell builtin and not a binary. But then, why does sudo echo 'this is a test' works fine? What ...
0
votes
1answer
58 views

How can I echo dollar signs?

For example, $PATH and $HOME When i type echo $PATH it returns my $PATH, but i want to echo the word $PATH and not what the actual variable stands for, echo "$PATH" doesn't work either. It might ...
1
vote
2answers
47 views

how to use http proxy in terminal

I have to use a http proxy to access Internet. Now I'm using firefox with the http proxy to access Internet. But on terminal, I can' access Internet. So are there ways to use http proxy on terminal ...
-1
votes
2answers
34 views

Matching multiple patterns with ls

I'd like to get a list of fail logs in the current directory for use in an outside script, the logs are determined with the pattern FAIL in the filename, so I've been using a FAIL* to feed my script ...
0
votes
1answer
72 views

How do I use vim on the command line to add text to the middle of a file?

I am trying to write a command along the lines of the following: vim -c "XXXXXX" myFile Instead of the "XXXXX" I want to supply some commands to vim to add some text to an arbitrary point in the ...