Questions tagged [scripts]

A script is a program or sequence of instructions written in a plain text file. The script interpreter (for example Bash, Python, Perl etc.) reads the file and carries out the instructions as if they had been entered at the command prompt. Use this tag for all questions related to creating, troubleshooting and running scripts.

Filter by
Sorted by
Tagged with
501 votes
16 answers
4.3m views

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right Click → Open With → Other Application... → Terminal. How do I open this file in the ...
Alex's user avatar
  • 5,011
477 votes
2 answers
379k views

Differences between /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin

I have six directories with command files. These are /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin and /usr/local/sbin. What are the differences between these? If I'm writing my own scripts, where ...
user avatar
416 votes
4 answers
873k views

What is the difference between #!/bin/sh and #!/bin/bash?

if I write, #!/bin/bash echo "foo" or #!/bin/sh echo "foo" both yields same. I have seen some scripts starting with #!/bin/sh or #!/bin/bash. Is there any difference between them?
Rahul Virpara's user avatar
381 votes
16 answers
730k views

How can I get a list of all repositories and PPAs from the command line into an install script?

I know how to list all packages installed on my system. But how could I get a list of all repositories and PPA's into a script that I can run on a new machine to replicate the repository setup ...
stwissel's user avatar
  • 6,211
300 votes
7 answers
441k views

ssh: automatically accept keys

I've written this small utility script: for h in $SERVER_LIST; do ssh $h "uptime"; done When a new server is added to $SERVER_LIST, the script is stopped with: The authenticity of host 'blah.blah....
Adam Matan's user avatar
  • 12.5k
231 votes
8 answers
745k views

How can I make a script that opens terminal windows and executes commands in them?

I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment. To do that, I manually open three terminals and type in the commands. ...
JSBach's user avatar
  • 2,481
210 votes
7 answers
415k 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 ...
user192118's user avatar
  • 2,101
209 votes
11 answers
728k views

How can I create a select menu in a shell script?

I'm creating a simple bash script and I want to create a select menu in it, like this: $./script echo "Choose your option:" 1) Option 1 2) Option 2 3) Option 3 4) Quit And according to ...
Daniel Rodrigues's user avatar
206 votes
8 answers
682k 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 got this error: ./create_mgw_3shelf_6xIPNI1P....
user165062's user avatar
  • 2,123
198 votes
11 answers
369k views

How to run an alias in a shell script?

I have an executable file mpiexec, whose full path is ~/petsc-3.2-p6/petsc-arch/bin/mpiexec. Since I want to execute this command in different directories (without having to retype the entire path), ...
Paul's user avatar
  • 2,219
197 votes
3 answers
524k views

Command to append line to a text file without opening an editor

Assuming i have a line that i want to add to a file without opening an editor. How could i append this line alias list='ls -cl --group-directories-first' to this file config.fish
NES's user avatar
  • 33.2k
191 votes
8 answers
368k views

Repeat a command every x interval of time in terminal?

How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ? There is no need for a script, i need just a simple command ...
user avatar
171 votes
2 answers
67k views

What is the difference between "source" and "."?

What is the difference between executing a script (e.g. /some/script) with source /some/script and . /some/script in Bash?
binW's user avatar
  • 13k
170 votes
11 answers
186k views

How can a script check if it's being run as root?

I'm writing a simple bash script, but I need it to check whether it's being run as root or not. I know there's probably a very simple way to do that, but I have no idea how. Just to be clear: What's ...
Malabarba's user avatar
  • 10.2k
167 votes
5 answers
561k views

'chmod u+x' versus 'chmod +x'

What is the difference between chmod u+x and just chmod +x? I have seen a ton of tutorials that say to use u+x to make scripts executable. However, omitting the u doesn't seem to have any effect.
Nathan Schwermann's user avatar
166 votes
2 answers
211k views

What does outputting to /dev/null accomplish in bash scripts? [duplicate]

Oftentimes I run into small bash scripts that use this sort of syntax in if statements: some command > /dev/null 2>&1 What is the purpose of outputting to /dev/null like that, and what does the 2>...
javanix's user avatar
  • 1,793
159 votes
13 answers
372k views

How to execute a script just by double clicking like .EXE files in Windows?

How can I make a bash script executable by double clicking just like .exe files in Windows? I tried creating a launcher and assigning the script to it, but there are two consequences: the terminal ...
user avatar
152 votes
7 answers
630k views

How do I run a '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] ...
user251948's user avatar
  • 1,521
141 votes
2 answers
324k views

How to add a shell script to launcher as shortcut [duplicate]

Possible Duplicate: How can I edit/create new launcher items in Unity by hand? How can I create launchers on my desktop? I want to add some shell scripts in my launcher as shortcuts.. They are ...
Nirmik's user avatar
  • 7,858
139 votes
5 answers
542k views

How do I run a script at start up? [duplicate]

I have a script in a folder: /path/to/my/script.sh I need this script to run every time the system starts (even if no one logs in to the system). What do I need to do in order to make this happen?
Rusty's user avatar
  • 1,541
133 votes
10 answers
395k views

Bash remove first and last characters from a string

I have a string like that: |abcdefg| And I want to get a new string called in someway (like string2) with the original string without the two | characters at the start and at the end of it so that I ...
Matteo Pagliazzi's user avatar
130 votes
13 answers
173k views

Script to display all terminal colors

Throughout the internet I have seen many people with scripts that prints out a bunch of stuff in all the colors defined in ~/.Xdefaults. However when I try to use these, I always get error: Bad ...
John Howard's user avatar
  • 2,035
123 votes
4 answers
555k views

What is the meaning of exit 0, exit 1, and exit 2 in a bash script?

I'm doing some practice exercises. Write a script that will be given a month number as the argument and will translate this number into a month name. The result will be printed to stdout. I made a ...
andrej benedičič's user avatar
113 votes
5 answers
177k views

Is there a standard place for placing custom Linux scripts? [duplicate]

I am developing a set of scripts, which I want to be present for all users, when I set up a server. But I don't want them in the standard locations like /usr[/local]/[bin],[sbin] etc. Is there some ...
vfclists's user avatar
  • 1,849
109 votes
12 answers
500k views

How do I read a variable from a file?

I want to insert in my script a value (string) that I would read from a text file. For example, instead of: echo "Enter your name" read name I want to read a string from another text file so the ...
user208413's user avatar
  • 1,125
107 votes
17 answers
152k views

Beep in shell script not working?

I'd like to use a beep sound in a shell script. Unfortunately none of the methods I found via Google work for me. I tried echo -e '\a' echo -ne '\007' and the command beep after I installed it via ...
NES's user avatar
  • 33.2k
105 votes
5 answers
427k views

How to run a shell script in background?

How can I run a shell script in background?
erkan şentürk's user avatar
101 votes
4 answers
513k views

How can I make /etc/rc.local run on startup?

I have a script I would like to run when my system starts and have put it in /etc/rc.local, but it doesn't work. How can I enable it to run on startup?
pedroo's user avatar
  • 1,215
97 votes
5 answers
150k 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 if you ...
Anon's user avatar
  • 12.1k
96 votes
4 answers
252k 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 ...
Yanick Rochon's user avatar
95 votes
4 answers
424k views

How to execute a script in a different directory than the current one?

I tried executing a script with this command: ./home/user/scripts/someScript but it doesn't work: No such file or directory Any ideas?
UAdapter's user avatar
  • 17.6k
95 votes
5 answers
73k 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?
Fat Mind's user avatar
  • 2,445
94 votes
3 answers
840k views

What does "chmod +x <filename>" do and how do I use it?

I want to write the Ubuntu analogue of a "batch file" (a shell script). But I don't know how to use chmod +x filename command to make it so that the script can be run. Nor do I know where to use it.
user265696's user avatar
90 votes
8 answers
52k views

How do I create a deb package for a single python script?

I have a single python script that I want to distribute as a deb package. It is a indicator which shows local date in Unity panel. I did follow create a .deb Package from scripts or binaries but I ...
sagarchalise's user avatar
87 votes
8 answers
274k 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 ...
user128712's user avatar
  • 2,362
85 votes
5 answers
119k views

Is bash scripting the same as shell scripting?

I'm very new to Ubuntu. I hear people say "shell scripting", "bash scripting". I wonder are they same? Or they are different?
batman's user avatar
  • 7,961
85 votes
1 answer
53k 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 ...
Yunyu L.'s user avatar
  • 953
84 votes
8 answers
439k views

How to run scripts every 5 seconds?

I have a script that needs to be run every five seconds. I know that cron can do tasks by the minute, but is there a way to run something every second?
myusuf3's user avatar
  • 34.2k
84 votes
3 answers
84k views

Displaying a new line on the prompt

I wish to add a new line on every prompt. Instead of the cursor showing on the line as name@machine:~$ _, I want it to be on the next line, like as: name@machine:~$ _ Thanks.
theTuxRacer's user avatar
  • 16.2k
84 votes
6 answers
136k views

Cannot successfully source .bashrc from a shell script

Normally we can source ~/.bashrc file using this command source ~/.bashrc but if I write this in a shell script and execute it, nothing happens. Why? Is there any way to do this? My script: #!/bin/...
shantanu's user avatar
  • 8,589
84 votes
4 answers
40k 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 ...
Patterson's user avatar
  • 1,360
82 votes
3 answers
124k views

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

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 ...
Pandya's user avatar
  • 35.7k
82 votes
6 answers
61k views

What is the difference between ./ and sh to run a script?

I have written a simple script. When I runsh <myscriptname.sh>, i got the correct output, but when I run ./<myscriptname.sh>, I got an error. What is difference between when I do sh and ....
Vishwanath Dalvi's user avatar
79 votes
3 answers
212k views

Hiding output of a command

I have a script where it checks whether a package is installed or not and whether the port 8080 is being used by a particular process or not. I am not experienced at all with bash, so I did something ...
dabadaba's user avatar
  • 1,155
79 votes
1 answer
519k 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
R Square's user avatar
  • 959
76 votes
9 answers
108k views

Rotate images from terminal

I have a directory with a lots of images but they are in the wrong orientation. I want to rotate the images to correct the orientation (mostly ±90o). Using image (shotwell photo) viewer I can ...
pranphy's user avatar
  • 1,608
76 votes
2 answers
17k views

Can I make the webcam take a picture when an incorrect password is entered?

I am using a laptop . I was thinking to have a support in my Ubuntu machine. When an incorrect password is entered, a picture is taken later revealing who has been trying to gain access. I have ...
twister_void's user avatar
  • 5,924
74 votes
5 answers
248k views

how to check if $1 and $2 are null?

I am running some script which passing the string argument and I want to do if else statement shown as below: if [ $1 != '' ] && [ $2 != '' ] then do something..... but it shown Error ...
taymindis  Woon's user avatar
73 votes
10 answers
119k views

How to run scripts without typing the full path?

I'd like to know if there is a way to run program/shell script without typing full path: /opt/idea/bin/idea.sh
Poorman_Patrick's user avatar
73 votes
4 answers
39k views

Is #!/bin/sh read by the interpreter?

In bash or sh, I guess anything which starts with # is a comment. But in bash scripts we write: #!/bin/bash And in Python scripts, there is: #!/bin/python Does this mean that # by itself is a ...
Gaurav Sharma's user avatar

1
2 3 4 5
134