How do I create a script file for terminal commands?
Solution 1
There are two methods.
First, the most common is to write a file, make sure the first line is
#!/bin/bash
Then save the file. Next mark it executable using chmod +x file
Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make them end in .sh
or any other way.
A few notes:
- Any (and I mean any) file can be executed in Linux provided the first line is a path to the program that should interpret the file. Common examples include
/bin/python
,/bin/sh
,/bin/dash
, but even odd ball things work like/bin/mysql
- Bash is a full language. It is vastly more complex than cmd.exe in windows. It has a strong programming language that supports functions, loops, conditionals, string operations, etc.
- These documents may help if you run into problems.
- If you do not wish to make the file executable then you can run it by passing it as an argument to bash:
bash file/to/run.sh
A Simple Bash Example
#!/bin/bash
echo "This is a shell script"
ls -lah
echo "I am done running ls"
SOMEVAR='text stuff'
echo "$SOMEVAR"
The second method is to record commands using script
. Run script
then just do stuff. When you are done doing stuff type exit
and script will generate a file for you with all the "stuff" you did. This is less used but works quite well for making things like macros. man script
for more info.
Solution 2
You mean writing to a file using a shell script? Here are a few ways:
touch file
This method will simply create a file, but if the file already exists, it simply changes the modification date to the time you used that command.
echo "text" > file
That method overwrites the contents of file
to text
. If you wanted to clear a file, you can simply do this:
echo "" > file
Say you want to write more than one line to it, and you don't want to use thousands of echo
commands, you would use this command:
cat << EOF > file
test
test1
foo
bar
EOF
That enables you to write multiple lines in one command. The contents of file
would then be this:
test
test1
foo
bar
If you wanted to append to a file, replace >
to >>
.
Hope this helps!
EDIT: Oh, I see, so you would write the file in gedit, using the .sh
extension (optional, but it's a good idea), and then on a file manager, right click on the file, select Properties->Permissions, and check Allow executing file as program
. Then you can double-click on it and it will run :). Also, if you want to do so in the terminal, you can run this command to make it executable (you might want to prepend sudo
if it doesn't belong to you):
chmod +x file
And to run:
./file
Solution 3
The equivalent to Windows batch files is shell scripts, and an excellent getting started guide is Bash Scripting.
For the most part, commands that you can enter on the command line can be placed in a shell script.
A couple of things that are different from Windows batch files:
There are different command interpretors, called shells. The default is bash, but if you are interested, there are others, such as zsh, ksh, dash, perl, python, etc.
To run a shell script, you need to make the file executable, which you can do with
chmod +x <filename>
In Ubuntu, the current directory is not the program search path, so you need to run
./<filename>
, not<filename>
Variable names are
$<varname>
, not%<varname>%
Commands in a shell script are not printed by default, as in a batch file.
The filename's extension can be
.sh
or (more customary) you don't need to use an extension. Put#!/bin/bash
on the very first line of the file, which tells Ubuntu what program to use to run the file.Comments start with
#
, notrem
.
Hope this helps and have fun scripting!
Related videos on Youtube
web.learner
Updated on September 18, 2022Comments
-
web.learner over 1 year
In Windows I can write a file containing commands for cmd (usually
.cmd
or.bat
files). When I click on those files it will opencmd.exe
and run them commands the file contains.How would I do this in Ubuntu?
I'm sure this is a duplicate, but I can't find my answer.
Its similar to these questions, but they don't answer the question: -
web.learner over 11 yearsActually what I want to do is write a file in, say gedit, that contains terminal commands. Then when I double click this file it will run those commands in the terminal.
-
TryTryAgain over 11 yearsGreat answer, considerate update. To simply create a file, I've always just used
touch filename
-
foodiepanda over 11 years@TryTryAgain, yep that is an easy method, probably faster than
echo "" > file
. Is it ok if I include that method in my answer? -
TryTryAgain over 11 years@MiJyn Absolutely include it, feel free. Thanks
-
TC1 over 11 yearsYou might wanna change that
/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu. -
Carlos Campderrós over 11 years@TC1 It's installed by default, so it doesn't matter if it's the default or not.
-
foodiepanda over 9 yearsI'd like to point out that
/bin/dash
(which/bin/sh
is usually symlinked to), is a lot faster than bash (I measured approximately 15 times faster). If at all possible, use/bin/dash
for shell scripts :) -
Elia Weiss almost 9 yearsI get 'command not found' even after chmod
-
coteyr almost 9 yearsIf your in the same folder as the file make sure to ./file.sh
-
George over 8 yearsI was using a space between VARIABLE the
=
sign and the variable content, and that was giving me the "command not found" error. It's a detail that might create some problems. -
John Joe over 7 yearshow to save the file?
-
coteyr over 7 yearsby selecting "Save" in your text editor, or by exiting the script command with the exit keyword.
-
Omer almost 7 yearsHow can I make it to run any where in terminal ... like a global command?
-
ckapilla almost 7 yearsif you get command not found and you are in the correct directory, you may need to type ./<file name> if your PATH doesn't include current directory (.). (There is a period before the slash that is pretty hard to see.)
-
foodiepanda almost 7 years@Omer add it to a directory in
$PATH
, like/usr/bin
:) probably best if you make it a link, likeln file /usr/bin
, instead of copying it, because if you copy it and it has references to files in the same directory, it'll look in/usr/bin
instead of the folder the executable should've been in. -
Enrique over 6 years@coteyr great answer. Which folder should I use to put the .sh file created so I can call it from anywhere and get the commands inside executed ? thanks...
-
coteyr over 6 yearsthat depends a lot on system setup,but usually /usr/local/bin is a good place to start as is $HOME/bin depending on what your trying to do. Any folder in $PATH should work just fine, or you can edit your $PATH
-
thymaro about 6 yearsI tried both methods, but if I try to double-click the bash script file, it just opens the text file in geany/gedit ready for editing. I'd like the script to be executed upon double-click. This question holds more detail as to what I want to do.
-
Lakshman Pilaka almost 6 yearssuch a beautiful explanation... kudos...
-
Salman Amintabar over 5 yearsHow can I create and run a command with param as a string inside a script?
-
icl7126 about 5 yearsIs this answer still valid in 19.04? The file is still being opened by text editor.
-
coteyr about 5 yearsYep, still valid, the only thing that might change is using ash, zsh, dash, sh, or whatever.