sp = "!echo name && read name"
I've added this to my aliases list. When I run it "name" is printed as expected and I can input something into read; but on carriage returning and trying echo $name
, nothing's there.
It works fine when I directly run the command in the terminal and whatever I type in gets stored in $name
.
I also have another problem which was the first one and I was trying to simplify it as much as I can to zone in on the issue above but here's the original:
sp = "!read branch\"?Branch Name: \""
Whenever I run this via git sp
, I get the error
read branch"?Branch Name:": line 0: read: `branch?Branch Name:': not a valid identifier
Copy pasting the first bit of the error which is the command text into the terminal and directly running it works perfectly so it can't be some sort of invalid hidden character somewhere tho I can't be certain how git is replacing stuff.
I'm on macos monterey using terminal.app and zsh. Thank you.
CodePudding user response:
[With the original desired alias, you get:]
read branch"?Branch Name:": line 0: read: `branch?Branch Name:': not a valid identifier
Git runs aliases by feeding them to /bin/sh
. The:
read var?"prompt string"
syntax is peculiar to the zsh read
built in (see, e.g., https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html); in /bin/sh that's an attempt to read into a variable named var?prompt string
, which is not a valid variable name because of the embedded question-mark and space.
To use a zsh script directly, make the script executable, place it in your $PATH
, and name it git-sp
. Then:
git sp
will run your git-sp
script. Since your script will begin with #! /bin/zsh
, you will get zsh and not plain sh, and you will have access to your desired read
built-in. Also, you won't have to deal with all the complications produced by wedging the entire command in as a single line alias:
When I run [a simplified version,] "name" is printed as expected and I can input something into read; but on carriage returning and trying
echo $name
, nothing's there.
Each command is run by a separate shell invocation. Values read into variables persist until that shell invocation exits (or you override or unset them in that same invocation); then they evaporate. To store something more permanently, use the file system. Consider storing settings in the git config
databases (--global
and per-repository): you can make up your own names here. You do run the risk that some future Git release (git version 3.1415
?) might steal your name and use it for its own purposes, but depending on what namespace you choose, you can make that pretty unlikely. For instance:
git config --global torek.user.setting1 value1
git config --global torek.user.setting2 value2
I'm only in trouble here if future-Git decides to use torek.*
settings. (Perhaps it's being worked on by a Slovenian on a Tuesday.)