I'm writing this script to work as a fast draft. It opens my text editor with date and time, so I can insert some draft text to save quickly.

1. At first I'd like to append the date and time at the beginning of the file instead of at the end, as >> $file does. But I guess that it is not so easy, so this is not my priority.

2. I defined an argument $1. If I have only few words to insert I believe that it is better to run draft "foo foo" instead of opening the editor to write just few words.

Well, it is working but with the argument the last line is not necessary. So I'd like to make it a conditional line. If I pass no argument, then open editor; else none.

Any help to improve my script?

#linha=$(wc -l < $file)
#rm $folder$file
printf "\n\n" >> $file
#echo " " >> $file
echo "------< $(date "+%b %d, %Y - %H:%M:%S") >------" >> $file
echo "$1" >> $file
exec leafpad  $file
  • Use a version control system, it will keep track of such stuff for you.
    – vonbrand
    Jan 21, 2013 at 17:49

2 Answers 2


You can use if to check. For example, you can do something like this instead of the last two lines in your script above:

if [ -n "$1" ]; then
  echo "$1" >> $file
  exec leafpad $file

This says: if the first argument is not an empty string (this is what -n test does), then run echo, else run leafpad.

You can read more about this here:


To be more generic, use "$@" instead of "$1" -- this lets you write

draft use many words without quoting.
printf "\n\n------< %s >------\n" "$(date "+%b %d, %Y - %T")" >> $file
# if any arguments were given, write them to the file, else edit the file
if [[ $# -gt 0 ]]; then
    echo "$@" >> $file
    exec leafpad $file

To write a line at the beginning of the file, you can use a grouping construct:

{ echo "first line"; cat $file; } > temp && mv temp $file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .