I'm trying to make an alias to quickly compile and upload Arduino sketches. I started by adding the location and fqbn of my arduino as shell variables, but that slowed down each shell start up, so I decided to add a check instead, and only export the variables if they aren't already exported. Now, it seems that the script isn't accessing the variables even though I can echo them after the function for exporting them has run. The error from the shell seems to indicate that the variables are still empty when they are called by the script, even if I run it a second time in the same shell. Here's the functions and alias which I'm referring to:
function dweeno() {
export DWEENOFQBN="$(arduino-cli board list | grep USB | awk '{print $(NF-1)}')"
export DWEENOLOC="$(arduino-cli board list | grep USB | awk '{print 1ドル}')"
}
function dweenocheck() {
if [ -z $DWEENOFQBN ]; then
dweeno
fi
}
alias acu="dweenocheck && arduino-cli compile --fqbn $DWEENOFQBN 1ドル && arduino-cli upload -p $DWEENOLOC --fqbn $DWEENOFQBN 1ドル"
1 Answer 1
Your alias uses the variables DWEENOFQBN etc., but at the time you define the alias, the variable has no value yet. You can verify this by setting
set -u
on top of your script.
Another problem is your use of 1ドル, which also does not have a value when the alias is defined. Hence, your alias definition effectively is
alias acu="dweenocheck && arduino-cli compile --fqbn && arduino-cli upload -p --fqbn"
You could get rid of of the problem with your exported variables by using single quotes for the alias definition, but this would still not solve the trouble with 1ドル. Positional parameters don't make sense in an alias.
Hence the only sane solution would be to turn the alias into a function.
$DWEENOFQBNwould have been used in the definition, rather than the current value, and the parameter would expand each time the alias is expanded. That said, you still want to use a function instead, because1ドルwould be expanded using the current positional argument, not whatever argument followed the alias on the command line.