Mac Shell script get home directory

Rafael Jimeno picture Rafael Jimeno · Jun 22, 2012 · Viewed 20.9k times · Source

I need to get the home directory in my shell script so my coworkers can run it. What my shell does is really simple, it only copy some directories to another. I've used:

$HOME,
$(whoami)
even this:
ABSPATH=$(cd "$(dirname "$0")"; pwd),

but when I use the variable like this:

DIR= $ABSPATH/folder/afolder/bfolder/

and run it I receive this:

/Users/theUser/Desktop/FusionTest.command: line 14: /Users/theUser/Desktop/folder/afolder/bfolder: is a directory
Copying to usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory logout
[Process completed]

I'm using the command cp -r to copy all files and directories.

Whay I am missing? or how can I do this??

Answer

You picture You · Jun 22, 2012

As the error message implies -r is the incorrect flag for recursive copying. The flag you want is -R (flags are case-sensitive). As for the home directory, $HOME should always work.