0

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
    Though looking at the whole thing, you'd be better of ditching the single quotes inside the alias and instead using single quotes for the alias: 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 – muru Sep 2 '19 at 17:29
  • 1
    Your variables $doa, $dob, $a, and $b are being evaluated when you define the alias, not when you use it. – roaima Sep 2 '19 at 17:32
0

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"
    )
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.