In shell scripting, a shell such as bash runs a file containing a list of commands (a script). The script is effectively a program and the shell is its interpreter.

learn more… | top users | synonyms

43
votes
4answers
3k views

When to use () vs. {} in bash?

I'm studying shell scripting with bash and I need to know the difference between (...) and {...}. How does one select between the two when writing a script?
32
votes
3answers
3k views

Should I save my scripts with the .sh extension?

I have some functional scripts and I want to copy to /usr/bin I want to use them as normal terminal commands. Is it a good practice to use them with the .sh extension or can I save them without ...
23
votes
3answers
3k views

Where should I put my script so that I can run it by a direct command?

Where can (should) I put my (bash) script so that it can be used (forever) by terminal or by a direct command: Alt+F2? I know there is /usr/bin and /sbin & /bindirectories but when should I use ...
20
votes
2answers
51k views

How to read complete line in 'for' loop with spaces

I am trying to run a for loop for file and I want to display whole line. But instead its displaying last word only. I want the complete line. for j in `cat ./file_wget_med` do echo $j done result ...
20
votes
2answers
2k views

Know whether the shell script contains syntax error without running the script

I have a long shell script containing a lot of conditions and I want to know whether there is any syntax error in the script without running it. Since shell script is interpreted I think it is not ...
19
votes
10answers
1k views

How to write a shell script to assign letter grades to numeric ranges?

I want to make a script that would prompt you for a number between 0-100 and then give you the grade based on the number. I'd like it in bash. PS3='Please enter your choice: ' (Something here) do ...
19
votes
6answers
54k views

Not able to execute a .sh file: /bin/bash^M: bad interpreter

I wanted to execute a shell script: -rwxr-x--x 1 root root 17234 Jun 6 18:31 create_mgw_3shelf_6xIPNI1P.sh I tried to do a standard procedure, but I am got this error: ...
18
votes
3answers
48k views

How do I run 'sudo' command inside a script?

To do a patch manually I must type this command sudo ./playback_delete_data_patch.sh 09_delete_old_data_p.sql There is a space just before the 09: sudo ./playback_delete_data_patch.sh [space] ...
18
votes
4answers
44k views

How to run a script during boot as root

I had a script that automatically enables my wifi without using networkmanager, but I don't know how to run the script as root while the system is booting. How do I make the script run automatically ...
16
votes
2answers
37k views

Why doesn't rc.local run all my commands, and what can I do about it?

I have the following rc.local script: #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # ...
16
votes
1answer
96k views

HTTP POST and GET using cURL in linux

I had server application in asp.net in windows in that i had a web service for that . how can i call web service in ubuntu using shell script by using cURL command
16
votes
1answer
2k views

How to display random jokes from www.icndb.com in your terminal?

I already know that you can get some nice quotes/jokes using the fortune program. But I've found in this urwird script a new source of fun that I'd like to have when I open a new terminal. This time ...
15
votes
6answers
3k views

Renaming hundreds of files at once for proper sorting

I have a ton of files, all named stuff like 1.jpg, 2.jpg, 3.jpg, and so on up to 1439.jpg, however, I have a problem with one of my projects and alphabetizing. It will usually go in the order 1.jpg, ...
15
votes
5answers
3k views

How to display a random line from a text file?

I am trying to write a shell script. The idea is to select a single line at random from text file and display it as an Ubuntu desktop notification. But I want different lines to be selected each time ...
14
votes
1answer
702 views

opposite of eject command?

I have a ton of CDs to image, and I just got curious. I can eject with the eject command. What command inserts the disk tray back into the CD drive? yes it is the type that can pull the tray back in. ...
13
votes
4answers
22k views

What does /dev/null mean in a shell script?

I've started learning bash scripting by using this guide: http://www.tldp.org/LDP/abs/abs-guide.pdf However I got stuck at the first script: cd /var/log cat /dev/null > messages cat /dev/null ...
13
votes
1answer
4k views

How do I add a PPA in a shell script without user input?

Basically, I want to run add-apt-repository ppa:(whatever) without the "press Enter to continue" prompt. How would I do this? Essentially, I want to deploy adding a repository in a shell script ...
13
votes
5answers
8k views

How do I limit Dropbox's activity on the hard disk?

On startup, Dropbox runs files indexing, which brings the system pretty much to a stand still for three minutes. Is there a way to restrict Dropbox from accessing the hard disk, or decrease the ...
12
votes
3answers
3k views

How to do: underline, bold, italic, strikethrough, color, background, and size in Gnome Terminal?

How to do: underline, bold, italic, strikethrough, and color in Gnome Terminal? Bold Italic u̲n̲d̲e̲r̲l̲i̲n̲e̲ s̶t̶r̶i̶k̶e̶ ̶i̶t̶ ̶l̶i̶k̶̶e̶ ̶i̶t̶s̶ ̶h̶o̶t Color background font < (its mono ...
12
votes
2answers
720 views

Is there an easy way to log all activity that a shell script does?

Is there an easy way to log all activity that occurs from a shell script to a file? I have a script. It outputs things like echo "instructions", as well as other program output. I know the commands: ...
12
votes
2answers
5k views

Anyway to make hotkey to turn screen off?

I was just wondering how can one make a script (or make some changes) by which I can turn screen off in Ubuntu(12.04) whenever I want, the way screen can be turned off in Cellphones. And also I would ...
11
votes
5answers
11k views

What does >> or double Angle brackets mean? [duplicate]

It seems like such a simple questions but I cant find any resource regarding it In C/C++ , it means to store the read in a value from command prompt and store it inside a variable , How about in ...
11
votes
1answer
867 views

Bash comparison and expression operators

I'm new to bash programming. I've read two good, long guides, but they made a mess in my head about operators and keywords. Which are more commonly and widely used? I don't know when to use which ...
10
votes
6answers
5k views

How to get the URL from a file using a shell script

I have a file which consists of a URL. I'm trying to get the URL from that file using a shell script. In the file, the URL is like this: ('URL', 'http://url.com'); I tried to use the following: ...
10
votes
3answers
9k views

Error message 'source: not found' when running a script

I am creating a little automated setup script that will modify /etc/profile and $HOME/.profile if some paths are not exported. Then, I'd like to reload these automatically. I have read that source ...
10
votes
2answers
471 views

How to run application for a set time in shell

If and How is it possible to, in Terminal, to get an application to run for 30 seconds, before being force closed, and echoing the logs of that process?
10
votes
2answers
4k views

Reliably check if a package is installed or not

I have a simple requirement. I want to define several variables that will correspond to any number of given packages I want to install via a shell script. Sample code below: MISC="shutter pidgin" ...
10
votes
2answers
267 views

Run only parts of a script as sudo, entering password only once

I am writing a script (actually a combination of scripts and makefile) that installs external dependencies: some apt-get commands, some git cloning, building, installing, adding users to groups, ... ...
10
votes
2answers
276 views

Auto clear search term from lens

There must be a setting or a shell script that would clear the terms from the lens every time it's closed. The behaviour would be: open lens and type a term. reopen the lens with super key. see ...
9
votes
2answers
789 views

Case sensitivity in shell scripting

Consider this Bash script: #!/bin/bash echo Enter any character read char case $char in [a-z]) echo Lower case letter ;; [A-Z]) echo Upper case letter ;; [0-9]) ...
9
votes
2answers
2k views

What does the first '.' mean in '. ~/.bashrc'? [duplicate]

I saw a post about fix your alias in .bashrc. And he says after you put your alias in .bashrc , you need to use: . ~/.bashrc I do not quite understand what the first dot(' . ') does here. What's ...
8
votes
5answers
567 views

How do you output the filename in find command while using -exec?

I use the following line to find all sub-directories of the PWD and run svnadmin verify on each directory (I already know that they're Subversion repositories) find ./* -maxdepth 0 -exec svnadmin ...
8
votes
7answers
4k views

Exit terminal after running a bash script

I am trying to write a bash script to open certain files (mostly pdf files) using the gnome-open command. I also want the terminal to exit once it opens the pdf file. I have tried adding exit to the ...
7
votes
5answers
997 views

How to send a file as an input to another file?

Through the command shell we can redirect the three standard filestreams so that we can get input from either a file or another command instead of from our keyboard, and we can write output and errors ...
7
votes
5answers
547 views

how can I take total minutes from time?

My system displayed the time like this 10:42 when i input the command date +%R. I need to take in total minutes of the time. That means like this 642 minutes. Is there any command or shell script for ...
7
votes
2answers
3k views

Difference between ls -l and ll?

I'm relatively new to programming as a whole and some tutorials have been telling me to use ls -l to look at files in a directory and others have been saying ll. I know that ls is a short list, but is ...
7
votes
3answers
1k views

Why do commented lines in a linux configuration file sometimes work? [duplicate]

By a commented line I mean either: #!/usr/bin/perl -w and the comments in /boot/grub/menu.lst file as described in this answer on superuser. It already has a comment, i.e the line begins with a ...
7
votes
3answers
13k views

Store output of command into the array

This is command: pdc status -a 2>&1 | grep 'okay' It gives the following output [okay ]: you are currently listening: 33 [okay ]: you are currently listening: 22 [okay ]: you are ...
7
votes
1answer
37k views

How to make a file executable? [duplicate]

How can I make my file so that I can double click on it, and it runs. It is a .sh script, but I also have files that say: executable (application/x-executable) in the description of what they are. ...
7
votes
2answers
764 views

Batch files renaming w/ filename's shift

I have a ton of files named this way: [name]_[phonenumber]_HH-mm-ss_dd-MM-yyyy.mp3. How can I shift [name] and [phonenumber] to the end of file name, and put the date in the beginning in form of ...
7
votes
1answer
8k views

How to run a command at login?

I need to run the following command (with real parameters) after login: ssh -f user@example.com -L 54321:example.com:54321 -N I need it only for my user, not all users on the machine. Also, I need ...
7
votes
3answers
5k views

What is the difference between “source x”, “. x” and “./x” in Bash?

I have one bash source run.sh as follows, #!/bin/bash if [ $# -ne 1 ]; then exit fi ... when I execute it in two ways, there are different behaviors. The first way is, source run.sh It will ...
7
votes
3answers
542 views

How can I start up an application with a pre-defined window size and position?

I'm wondering is there any way to achieve the affect of the Ctrl-Alt-Keypad shortcuts in Unity using terminal commands instead? I want a command that sets a gui window to half the size of the screen, ...
6
votes
2answers
1k views

Execute command from file in current shell

I want to executed command from file (which is script) in current shell in terminal. Example: I have file ch_dir contains following: #!/bin/bash cd /usr Now if I execute file usually as following ...
6
votes
2answers
41k views

How to run shell script via terminal using ubuntu 13.04?

I just upgraded to 13.04. Before this when I click on a shell script file it'll give an option to run via terminal but now the option is no longer available and when I click the file it'll directly ...
6
votes
3answers
166 views

Getting internet browsing history from shell

is it possible to use the shell or generate a shell script to find out the browsing history from the browsers installed on the system? thank you
6
votes
2answers
11k views

Using a variable inside `sed` command

I am using this command to copy certain line from one file to another.Its working fine.No issue with it. sed -f <(sed -e '1,10d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ...
6
votes
3answers
9k views

How can I process multi-line records with awk in a bash script?

example.txt is below Restaurant: McDonalds City: Miami State: Florida Address: 123 Biscayne Blvd Phone: 911 Restaurant: 5 guys City: Atlanta State: Georgia Address: 123 Peachtree Rd Phone: 911 ...
6
votes
1answer
295 views

Shell scripting bash: How to detect Alt+N keystroke?

I want to write a script that always listens in the background and execute a particular command when the user hits a specific key combination such as Alt + N. How this can be done?
6
votes
2answers
172 views

Harmless pranks there for the command line? [closed]

Seeking harmless pranks or April fools for Ubuntu (one-liners), with which I can bring my colleagues laugh. However, it should not damage the system. To broad, ok… Only shell scripts With a maximum ...