In my .bash_profile
file, I have this, which works fine to execute that script in the same process that I'm launching my shell...
. ~/Code/iOS/CommunityDemo/APIMocker/mockutil.sh
I realized for part of that script, I need the path where it resides to be used within it. Normally, you'd just do this...
MOCKUTIL_PATH=$(dirname "$0")
However, since I'm not spawning the script into its own process, but rather including the script in my profile script, the above understandably yields /bin
, not ~/Code/iOS/CommunityDemo/APIMocker
as I need.
"No worries!" I figured... I'd just export the path from within my profile, like this...
export MOCKUTIL_PATH="~/Code/iOS/CommunityDemo/APIMocker"
. ${MOCKUTIL_PATH}/mockutil.sh
but I'm getting this...
bash: ~/Code/iOS/CommunityDemo/APIMocker/mockutil.sh: No such file or directory
Now I can just do this, but I really hate redundancy!
export MOCKUTIL_PATH="~/Code/iOS/CommunityDemo/APIMocker"
. ~/Code/iOS/CommunityDemo/APIMocker/mockutil.sh
So... is there a way to define that path once, then use that definition in the source
(.
) command?
$HOME
instead of tilde~
expansion."$HOME/Code/iOS/CommunityDemo/APIMocker"
– Jetchisel Apr 09 '20 at 23:21export MOCKUTIL_PATH="~/long/junk"
(where it's not needed and prevents the~
from being expanded), but "forget" to quote the variable it in. ${MOCKUTIL_PATH}/mockutil.sh
where it SHOULD be double-quoted (quoted, not put inside braces), otherwise a lot of funny stuff will happen. – Apr 09 '20 at 23:30export MOCKUTIL_PATH=~/long/path; . "$MOCKUTIL_PATH/mockutil.sh"
. – Apr 09 '20 at 23:34FOO="$VAR"
vsFOO=$VAR
vsFOO="${VAR}"
vsFOO=${VAR}
? I still don't quite get it. In other words, I'm not sure when you're supposed to use the curly braces vs when you're supposed to wrap it in quotes, or any combination of them. – Mark A. Donohoe Apr 09 '20 at 23:38echo "$VAR"
however is not identical toecho ${VAR}
orecho $VAR
: try withVAR='*'; echo ${VAR}; echo "$VAR"
– Apr 09 '20 at 23:40VAR='*'; echo ${VAR}; echo "$VAR"
and they returned identical results. Both printed '*'. That's why I'm confused. Again can you tell me what the difference is? I'm struggling to find it when googling. Kinda confusing. Using zsh on macOS 10.15.4 if it matters. – Mark A. Donohoe Apr 09 '20 at 23:44VAR='*'; echo $VAR
example demonstrates one of the most important differences. But except for that latter example, everything else I've said holds true in zsh too. – Apr 09 '20 at 23:48