How do I create a script file for terminal commands?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
95
down vote
favorite
In Windows I can write a file containing commands for cmd (usually .cmd
or .bat
files). When I click on those files it will open cmd.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:
Store frequently used terminal commands in a file
CMD.exe Emulator in Ubuntu to run .cmd/.bat file
command-line bash files executable
add a comment |Â
up vote
95
down vote
favorite
In Windows I can write a file containing commands for cmd (usually .cmd
or .bat
files). When I click on those files it will open cmd.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:
Store frequently used terminal commands in a file
CMD.exe Emulator in Ubuntu to run .cmd/.bat file
command-line bash files executable
add a comment |Â
up vote
95
down vote
favorite
up vote
95
down vote
favorite
In Windows I can write a file containing commands for cmd (usually .cmd
or .bat
files). When I click on those files it will open cmd.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:
Store frequently used terminal commands in a file
CMD.exe Emulator in Ubuntu to run .cmd/.bat file
command-line bash files executable
In Windows I can write a file containing commands for cmd (usually .cmd
or .bat
files). When I click on those files it will open cmd.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:
Store frequently used terminal commands in a file
CMD.exe Emulator in Ubuntu to run .cmd/.bat file
command-line bash files executable
command-line bash files executable
edited Apr 13 '17 at 12:24
Communityâ¦
1
1
asked Nov 30 '12 at 2:56
![](https://i.stack.imgur.com/fSaxx.jpg?s=32&g=1)
![](https://i.stack.imgur.com/fSaxx.jpg?s=32&g=1)
Sethâ¦
32.6k24109158
32.6k24109158
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
125
down vote
accepted
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.
You might wanna change that/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu.
â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
I'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 :)
â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
 |Â
show 8 more comments
up vote
14
down vote
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!
add a comment |Â
up vote
13
down vote
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
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just usedtouch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster thanecho "" > file
. Is it ok if I include that method in my answer?
â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
 |Â
show 3 more comments
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
125
down vote
accepted
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.
You might wanna change that/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu.
â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
I'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 :)
â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
 |Â
show 8 more comments
up vote
125
down vote
accepted
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.
You might wanna change that/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu.
â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
I'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 :)
â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
 |Â
show 8 more comments
up vote
125
down vote
accepted
up vote
125
down vote
accepted
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.
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.
edited May 23 at 7:23
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.9k55795
19.9k55795
answered Nov 30 '12 at 3:18
coteyr
11.7k52349
11.7k52349
You might wanna change that/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu.
â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
I'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 :)
â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
 |Â
show 8 more comments
You might wanna change that/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu.
â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
I'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 :)
â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
You might wanna change that
/bin/bash
to /bin/sh
, bash isn't even the default for Ubuntu.â TC1
Nov 30 '12 at 10:14
You might wanna change that
/bin/bash
to /bin/sh
, bash isn't even the default for Ubuntu.â TC1
Nov 30 '12 at 10:14
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
@TC1 It's installed by default, so it doesn't matter if it's the default or not.
â Carlos Campderrós
Nov 30 '12 at 14:23
1
1
I'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 :)â MiJyn
Oct 23 '14 at 17:17
I'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 :)â MiJyn
Oct 23 '14 at 17:17
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
I get 'command not found' even after chmod
â Elia Weiss
May 31 '15 at 7:01
4
4
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
If your in the same folder as the file make sure to ./file.sh
â coteyr
Jul 23 '15 at 18:51
 |Â
show 8 more comments
up vote
14
down vote
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!
add a comment |Â
up vote
14
down vote
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!
add a comment |Â
up vote
14
down vote
up vote
14
down vote
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!
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!
edited Dec 20 '13 at 23:10
![](https://i.stack.imgur.com/cFyP6.jpg?s=32&g=1)
![](https://i.stack.imgur.com/cFyP6.jpg?s=32&g=1)
Gilles
43.5k1398137
43.5k1398137
answered Nov 30 '12 at 3:14
iBelieve
4,23932054
4,23932054
add a comment |Â
add a comment |Â
up vote
13
down vote
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
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just usedtouch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster thanecho "" > file
. Is it ok if I include that method in my answer?
â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
 |Â
show 3 more comments
up vote
13
down vote
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
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just usedtouch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster thanecho "" > file
. Is it ok if I include that method in my answer?
â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
 |Â
show 3 more comments
up vote
13
down vote
up vote
13
down vote
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
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
edited Nov 30 '12 at 4:18
answered Nov 30 '12 at 3:00
MiJyn
2,6461425
2,6461425
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just usedtouch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster thanecho "" > file
. Is it ok if I include that method in my answer?
â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
 |Â
show 3 more comments
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just usedtouch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster thanecho "" > file
. Is it ok if I include that method in my answer?
â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
Actually 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.
â Sethâ¦
Nov 30 '12 at 3:05
1
1
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Check my updated answer :)
â MiJyn
Nov 30 '12 at 3:07
Great answer, considerate update. To simply create a file, I've always just used
touch filename
â TryTryAgain
Nov 30 '12 at 3:17
Great answer, considerate update. To simply create a file, I've always just used
touch filename
â TryTryAgain
Nov 30 '12 at 3:17
@TryTryAgain, yep that is an easy method, probably faster than
echo "" > file
. Is it ok if I include that method in my answer?â MiJyn
Nov 30 '12 at 3:19
@TryTryAgain, yep that is an easy method, probably faster than
echo "" > file
. Is it ok if I include that method in my answer?â MiJyn
Nov 30 '12 at 3:19
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
@MiJyn Absolutely include it, feel free. Thanks
â TryTryAgain
Nov 30 '12 at 4:15
 |Â
show 3 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f223691%2fhow-do-i-create-a-script-file-for-terminal-commands%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password