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
/bin/bash
to/bin/sh
, bash isn't even the default for Ubuntu. – TC1 Nov 30 '12 at 10:14/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=
sign and the variable content, and that was giving me the "command not found" error. It's a detail that might create some problems. – George Rappel Feb 03 '16 at 17:29