2

Often, I want to note down something really quicky when working in the terminal - Something to do, something interesting, etc. So I thought I'd include this function in my .bashrc:

export QUICKY="$HOME/Documents/Notes/quickies"
quicky(){
  if (( $# == 0 )) ; then
    vim $QUICKY
    return
  fi
  for i in $@; do
    echo "$i" >> $QUICKY
  done
}

So this is really simple. A use case would be something like quicky meeting to quicky note about a meeting or something similar. I would just do quicky to read the notes and further organize if I wanted.

Suppose I do something like quicky "Feed the dog", I expect a new line feed the dog in $QUICKY, but every word appears in a new line. How can I tackle this issue?

PS. quicky "!!" would be excellent to quicky note down interesting commands that you want to read about later.

Although suggestions and answers about any other ready-made tools are alright, I would like to know how I'd do this with bash.

  • echo "$@" >> $QUICKY will do; no need to loop. – sitaram Jun 15 at 10:05
  • @sitaram I need them separated by newlines – Suraaj K S Jun 16 at 5:38
  • huh! My English must be different from yours. To me, "but every word appears in a new line" means you did not want that. I'll just walk away from this; no need to reply – sitaram Jun 16 at 10:03
  • @sitaram Alright walk away... What I meant was quicky "feed the dog" "go to meeting" should have the two args on two different lines. Is that too difficult to understand? – Suraaj K S Jun 16 at 11:06
2

You need to quote your variables. Try this:

quickFile="$HOME/Documents/Notes/quickies"
quicky(){
  if (( $# == 0 )) ; then
    vim "$quickFile"
    return
  fi
  for i in "$@"; do
    printf '%s\n' "$i" >> "$quickFile"
  done
}

I changed your variable name so it isn't CAPITALIZED. It's generally better to avoid al caps variable names in shell scripts to avoid name clashes with environment variables. I also used printf since that is a better option for printing arbitrary data.

| improve this answer | |
  • A quick diversion... What happens if I use for i in $* instead of for i in $@ ? I mean, what is the technical distinction - they both seem similar to me... – Suraaj K S Jun 14 at 18:38
  • @SuraajKS see unix.stackexchange.com/questions/41571. – terdon Jun 15 at 0:03
  • huh? Wouldn't the \n in the printf cause the same problem OP asked to fix (i.e., each word appearing on its own line)? – sitaram Jun 15 at 10:19
  • @sitaram no, because here the $@ is correctly quoted as "$@". It is the quoting that fixes the issue, not the use of printf. – terdon Jun 15 at 10:24
  • Maybe I could also do (IFS=$'\n' ; echo "$*" >> $quickFile' ; ) ... (Although I have no idea why IFS="\n" just doesn't work) – Suraaj K S Jun 16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.