0

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?

  • 1
    Use $HOME instead of tilde ~ expansion. "$HOME/Code/iOS/CommunityDemo/APIMocker" – Jetchisel Apr 09 '20 at 23:21
  • You have you quotes backwards: you're quoting the value in export 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:30
  • Use export MOCKUTIL_PATH=~/long/path; . "$MOCKUTIL_PATH/mockutil.sh". –  Apr 09 '20 at 23:34
  • @pizdelect, can you elaborate on the difference between these: FOO="$VAR" vs FOO=$VAR vs FOO="${VAR}" vs FOO=${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:38
  • You're not using any of those, but they're 100% identical. echo "$VAR" however is not identical to echo ${VAR} or echo $VAR: try with VAR='*'; echo ${VAR}; echo "$VAR" –  Apr 09 '20 at 23:40
  • Just ran your code VAR='*'; 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:44
  • If you're using zsh, then why are you tagging your question [bash]? bash and zsh are VERY different shells, an the VAR='*'; 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
  • Because I thought I was using Bash. I just realized that 10.15 changed the default from bash to zsh. But you still haven't answered my question. That's ok. I'll see what I can find online. Thanks for helping. – Mark A. Donohoe Apr 10 '20 at 02:42
  • "But you still haven't answered my question" Yes, I have answered it. "I'll see what I can find online." Reading the manual would be better. As doing less of that passive-aggressive routine. It's not me who closed your question. I'm not reputable enough for that. –  Apr 10 '20 at 11:57
  • It's not passive-aggressive. You have not explained the difference. You keep saying 'Look!' and I keep saying 'At what? The output I'm seeing shows the same.' What I was looking for was something along the lines of 'When using a dollar with curly braces, it means it's doing xxx as opposed to using it without the curly braces, which simply means variable substitution. In both cases, using them within quotes is yyy', etc etc. That's what I have not been able to find. Plus, using the man pages (what I assume you mean by the manual)... what page am I supposed to look up?? – Mark A. Donohoe Apr 10 '20 at 22:50

0 Answers0