0

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"
2
  • I ended up just making a zsh script which works, but I'm still very curious as to why this wasn't working. Commented Apr 9, 2024 at 22:30
  • Note that if you had used single quotes to define the alias, the literal string $DWEENOFQBN would 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, because $1 would be expanded using the current positional argument, not whatever argument followed the alias on the command line. Commented Apr 10, 2024 at 18:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.