I recently added an alias to my .bash_aliases file:
alias runhole="perfect && cd data_series_test && doa=$(ls -1 | wc -l) && a=$(expr $doa / 2 ) && perfect && cd data_series_train && dob=$(ls -1 | wc -l) && b=$(expr $dob / 2 ) && perfect && python3 train.py > results_$b'_'$a"
and now when I open my terminal I have the error echoed twice:
expr: syntax error: unexpected argument ‘2’
expr: syntax error: unexpected argument ‘2’
I wanted to output a file called results_a_b where a and b are values defined in counting the files in folders defined in the alias, but instead the command outputs results__
1 Answer 1
Aliases are almost always better written as functions. The tricky part here is you chain every command together with &&
to abort early -- I use set -e
in a subshell here for the same effect.
runhole() {
( # run in a subshell to avoid side-effects in the current shell
set -e
perfect
cd data_series_test
doa=$( files=(*); echo "${#files[@]}" )
a=$(( doa / 2 ))
perfect
cd data_series_train
dob=$( files=(*); echo "${#files[@]}" )
b=$(( dob / 2 ))
perfect
python3 train.py > "results_${b}_$a"
)
}
alias runhole='perfect && cd data_series_test && doa=$(ls -1 | wc -l) && a=$(expr $doa / 2 ) && perfect && cd data_series_train && dob=$(ls -1 | wc -l) && b=$(expr $dob / 2 ) && perfect && python3 train.py > results_${b}_${a}'
Or just use a function$doa
,$dob
,$a
, and$b
are being evaluated when you define the alias, not when you use it.