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__

asked Sep 2, 2019 at 16:59
2
  • 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 Commented Sep 2, 2019 at 17:29
  • 1
    Your variables $doa, $dob, $a, and $b are being evaluated when you define the alias, not when you use it. Commented Sep 2, 2019 at 17:32

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.