I am writing a shell script, or I should more specifically say, a ZSH function, and I’m using various brief commands to check the status of things like git repositories, etc.
I’m having a lot of trouble with one particular command, however. In another stackoverflow posting I found the command to print the root folder of a git repository:
git rev-parse --show-top-level
In my actual zsh instance, if I execute
$(git rev-parse --show-top-level)
the appropriate value is returned and all is well. However, this approach inside my script is not working so well. I’ll give a little context:
[ $(git status &>/dev/null) $? -eq "0" ] && {
git_name=$("git rev-parse --show-top-level")
echo $(eval $git_name)
}
Now I don’t know about you, but this seems to me to be the wrong way to do this (not the first line; I’m all right with that). The output of this is roughly:
command not found: git rev-parse --show-top-level
The way it seems I should be writing this is:
[ $(git status &>/dev/null) $? -eq "0" ] && {
git_name=$(git rev-parse --show-top-level)
echo $git_name
}
The output of this is:
--show-top-level
and the exit status is 0.
My question is, why do neither of these work, and what would be the correct way to implement this? My guess is it has something to do with there being a double-dashed argument after a space, and the last long argument is being treated as a command itself (at least in the second example), but that’s the extent of my intuition.
Help appreciated!
Try this:
The double-quotes around the
git rev-parseare unnecessary and the argument should be--show-toplevel, not--show-top-level.