1

I am creating a basic bash script to create a blank file each day that I can use for taking notes. I'm naming the file with the current date and then trying to write it into a notes directory under my Documents folder. Everything works fine if I use the complete path but I'd rather use ~ instead of typing out my home dir.

This works:

notesPath="/Users/erik/Documents/RN/_DailyNotes/"

And this fails with a "no such file or directory" error:

notesPath="~/Documents/RN/_DailyNotes/"

I'm doing the following after creating notesPath:

todayFile=$notesPath$fileName
todayPretty=$(date "+%m/%d/%y")
echo "Daily $todayPretty" >> $todayFile                                         

Any ideas?

1
  • 1
    Are you running this script via a cron job? It's probably not executing as your user, so ~ is not resolving to your home folder. Commented Jul 28, 2014 at 1:07

1 Answer 1

6

Bash does not expand ~ inside of quotes. Try:

notesPath=~/"Documents/RN/_DailyNotes/"

For all the gory details on when and how ~ expands, see man bash, particularly the section entitled Tilde Expansion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.