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)

0
votes
4answers
226 views

How do I store long hostnames list for SSH quick usage?

I have an SSH access to a lot of servers. And all that hostnames (and sometimes usernames) are long and hard to remember. Also even if I tweak my Terminal to keep eternal bash history, the using of ...
22
votes
3answers
632 views

shellcheck is advising not to use basename: why?

I am trying out shellcheck. I have something like that basename "${OPENSSL}" and I get the following suggestion Use parameter expansion instead, such as ${var##*/}. From the practical point of ...
2
votes
4answers
187 views

How to start processing a file at an offset?

Use case: You've got a multi-GB log file for a whole week, and you need to search for something which happened on Saturday using for example grep. Making an educated guess, you assume that starting ...
0
votes
1answer
501 views

Less-than sign after done in a shell script

Should be easy but want to know why the “left arrow” < is used instead of right arrow. if fi done < a.txt What does it mean by left arrow? I thought since I am done with the ...
0
votes
2answers
993 views

Shell, bash, linux : after variable declaration, why semi-colon and what does it do?

Here is the line. variable=`ls -A $1 | grep '[abc]'; ls -1 $1` I understand the line before the semi-colon but I do not understand how this line works after semi-colon. Does it after the ...
-1
votes
1answer
4k views

$1 means for … ? without any input?

I have one program that starts with this line. What does this mean? I am having trouble googling because of the dollar sign. How come $1 without any parameters? And what does it mean by -d here? ...
1
vote
0answers
174 views

Serial Communication on DD-WRT stty config not sticking

I am attempting to control a serial device (in this case a projector) using a Fonera router with DD-WRT on it. I've got the serial port on the fonera wired through a level shifter and then into my ...
5
votes
7answers
954 views

What is the easiest way to add a string on the beginning of every line of the file from the command line?

I am looking for a way to add some string to the beginning of every line (same string for every line). Not something customizable but rather something that will be easy to remember and available on ...
3
votes
3answers
112 views

Is there something like closures for zsh?

I just decided to try zsh (through oh-my-zsh), and am now playing with precmd to emulate a two-line prompt that has right prompts in more than just the last line. So I clone the default theme, and ...
12
votes
0answers
356 views

Should I use $* or $@? [duplicate]

Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
0
votes
1answer
1k 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.
3
votes
2answers
642 views

Who is file owner if the file is created using sudo command?

I logged in using my username ravbholua: ravbholua@ravbholua-Aspire-5315:~$ echo $LOGNAME ravbholua I create file named a1: ravbholua@ravbholua-Aspire-5315:~$ echo>a1 ...
2
votes
2answers
104 views

Sourcing a shell script from within Emacs

I would like to source a shell script that changes my environment variables from within Emacs. That is, I would like the shell script to change the environment in which Emacs runs, and I would like to ...
3
votes
2answers
260 views

grep entire filesystem blocks on devices

Command grep -rHinF --colour=always -e "aa:aa:aa:aa" / Output grep: /dev/geom.ctl: Operation not supported by device grep: /dev/devctl: Device busy It seems that grep can not proceed because it ...
12
votes
3answers
418 views

How to display the number of lines output by a command in real time?

I'm using svn export as part of a packager script for my application, and it looks like this command, like many others, doesn't have any kind of progress bar. I have two choices at the moment: ...
3
votes
3answers
241 views

How to extract the file size with du

I'm using du to get the file size of the result archive in a packager script: > du -smh archive.zip 51M archive.zip I'd like to assign just the 51M part to a variable, to be able to print: ...
2
votes
1answer
90 views

How to restore from a scrambled screen?

My screen is scrambled while reading some unexpected chars, It happens in a local tty, so logging out won't help. Any ideas? Hopefully I don't have to reboot
2
votes
4answers
787 views

how to convert number to time format in shell script?

I want to cut a video into about 10 minute parts like this. ffmpeg -i video.mp4 -ss 00:00:00 -t 00:10:00 -c copy 01.mp4 ffmpeg -i video.mp4 -ss 00:10:00 -t 00:10:00 -c copy 02.mp4 ffmpeg -i video.mp4 ...
1
vote
1answer
2k views

How to write a shell script restart apache if `server reached MaxClients`

Recently, My blog always shutdown, it use centos6.2+apache2.2+mysql5.5+php5.3. I have raised MaxClients in httpd.conf, but less useful. So is it possible to write a shell script (Run with crontab ...
2
votes
2answers
370 views

BASH and Subshells

From what I gather from the online docs, the following should spawn a subshell for the part of the command embedded with {}: $ bash -c '{ sleep 10; echo "Sleeping process", $$; } & echo $$; ' ...
1
vote
1answer
91 views

What is the use of ; in a single line command?

What does ; mean in single line scripts like this: while true; do sudo -n true; sleep 60; kill -0 '$$' || exit; done 2>/dev/null & Does it mean new line, or "next command"?
3
votes
3answers
464 views

Rename multiple files, sub directories, and insert characters at specific location on OSX

I have files and directories starting with "cateory-icon-" in many sub directories. I am trying to rename them recursively to "category-icons-". Please note that I added missing "g" to the category ...
3
votes
1answer
147 views

Flatten shell script (include sourced scripts) and remove unused functions

I am manually tweaking Debian init scripts in order to optimize the boot time of an embedded device. So far I reduced it by half just with the "low hanging fruits" ie. the smaller scripts that are ...
4
votes
2answers
6k views

lftp: login, put file in remote dir and exit in a single command: proper quoting help

I want to use lftp -c to do an entire session in one go (as I'll be launching this from a script later on) and I managed with -e but that ofc leaves me with the interactive session which I don't want. ...
2
votes
1answer
96 views

Why does backslash get converted to space in echo statement if IFS='\'?

Why is \ is converted to space on setting IFS='\' var="'(]\\{}\$\"" IFS='\' echo $var # '(] {}$"
2
votes
2answers
3k views

Exporting a variable with dot (.) in it

How to export a variable which has dot in it. I get 'invalid variable name' when I tried : export my.home=/tmp/someDir -ksh: my.home=/tmp/someDir: invalid variable name Even escaping metacharacter ...
6
votes
1answer
1k views

gnome-terminal: keep track of directory in new tab

I'm on Arch linux, and when I open a new terminal tab, it always goes to $HOME. How can I make it so that when I open a new tab, it opens the shell in the directory I was in previously?
0
votes
1answer
156 views

What does this command-line do? [closed]

Please explain this in ways that I can understand. I'm very new to this stuff: ps ax >mytemp; more <mytemp … same as … ps ax | more cat mytemp | grep barrymp … same as … ps ax | grep “barrymp” ...
1
vote
1answer
466 views

How to prevent users from renaming files while providing write permissions on Linux [closed]

For some special purpose, I want to prevent non-root users of the Linux Server from changing or renaming the filenames. However, they can modify and write to the contents of the file. How to do this ...
0
votes
1answer
423 views

LD_LIBRARY_PATH not found - CentOS

I am trying to run the following two commands... LD_LIBRARY_PATH =/opt/oracle/instantclient_12_1 export LD_LIBRARY_PATH But when I run the first command I get an error -bash: ...
1
vote
5answers
883 views

Print nth line before the matched line, Matching line and nth line from the matched line

I want Print nth line before the matched line, Matching line and nth line from the matched line where "n" is greater than 2. Here's an example of my data file (the line numbers below are not part of ...
1
vote
2answers
629 views

Way to write new filename on wildcard input?

I have some experience with Inix terminals from science internships I've been part of, mainly using a few utilities like grep, awk, and sed but there is one thing I've been trying to figure out for a ...
3
votes
3answers
514 views

How to create a short path?

We have so many useful folders, which are in different path, for our working in Linux. For example : $ cd ~/Documents/Courses/EDX/physics2013/ $ cd ~/Documents/Company/OpenGL/Proj/... $ cd ...
0
votes
1answer
338 views

List of special characters in the shell [closed]

I am not sure what they are called, but where could I find a list of wild cards? I see things like: $f %s $1 Is there a definitive list for what each one represents?
1
vote
1answer
192 views

Is there a simple way of limiting the number of child processes that execute in parallel?

I have a bash script that kicks off multiple rsync processes in parallel. I'd like to limit the number of simultaneous child processes but not serialize the whole script. Here's a simplified version ...
2
votes
2answers
4k views

bash: syntax error near unexpected token `}'

I tried using this: $ if [$a == 1] then { echo 'yes'; } fi; but I get an error: -bash: syntax error near unexpected token `}' What is the correct format? I tried several with no luck.
13
votes
6answers
780 views

How to achieve portability with sed -i (in-place editing)?

I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...
5
votes
1answer
311 views

A program that runs in a shell (e.g. pine) vs. a shell script

Having a bit of trouble finding the answer to my question, perhaps because I may be asking the wrong question. I understand bash scripts. I write them all the time to do little helpful things ...
1
vote
3answers
739 views

cron: bash syntax not working

I am trying to add following command to crontab: I=1; for X in $(/bin/ls -r /var/tmp/*); do [ $((I++)) -le 28 ] && echo "lower" || echo "higher"; done When executed on the command line (in ...
3
votes
2answers
672 views

How to copy multiple files but keep their extensions the same?

I am looping through a number of files, and $file represents the current file. How can I make copies or renames and keep the extension the same for this current file? e.g. if $file = x.jpg How to ...
1
vote
3answers
194 views

Why brace expansion not supported?

Do I have to do any configuration to start brace expansion? When I run mkdir {1..10}, it just creates a dir naming {1..10}.
1
vote
3answers
797 views

How to add Multiple Searches in AWK command

Im trying to search for the files Created in June,July and August months. I used this method ls -lrth|awk '/[Jun][Jul][Aug]/ {print}' but it ain't working. Please help me correct the syntax.
1
vote
0answers
42 views

What's the difference between `` and $()? [duplicate]

When you want to return a command line output, you may consider using two methods: `cat file` or $(cat file). As far as I know, they are equivalent before my eyes. Is there a difference between both? ...
7
votes
3answers
2k views

which shell will sudo use to execute a shell script without the shebang line

My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1. First I login as a normal user: $ whoami fin $ cat /etc/passwd | grep -i "root\|fin" root:x:0:0:root:/root:/bin/bash ...
7
votes
1answer
2k views

Why didn't Ctrl-C work?

I just hit Ctrlc twice at my shell in an attempt to halt a process that is taking a long time to finish. ^C was echoed twice, but the process just kept going. Why didn't Ctrlc quit the process like ...
2
votes
1answer
2k views

Replace exact word with another in shell with tr command

I want to replace a word with another word in a text file. file.txt : alireza fallah my code : cat file.txt | tr ali ALI but I get this : ALI fALLAh it replace all 'a's to 'A' , and 'l's ...
0
votes
2answers
740 views

How to remove n number of lines from a file using sed command? [duplicate]

I want to remove a particular number of lines after the given pattern is matched. Can anyone please tell me how that can be done using the sed command ?
0
votes
2answers
258 views

Commands pasted into terminal are truncated

I have a list of commands I would like to paste into the terminal so that they run one after the other. Initially they run fine, but commands later on are being truncated. Example command: ogr2ogr ...
5
votes
2answers
215 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
111 views

generate links of pages with incrementing variable

consider the links http://unix.stackexchange.com/questions/tagged/linux?page=2&sort=newest&pagesize=15 ...