Skip to content
Advertisement

Syntax error when trying to pipe multiple commands in an alias definition

Hey can anyone help me to solve syntax error on creation of alias.I am new to bash so i am not aware of any restriction in use of alias command

Every time i run this alias command

alias user='responseVar=$(curl -XGET link)  && echo ${responseVar} | jq '.offsets[] | " (.topic) (.offset) (.logSize) "' | awk 'NR>1{arr[$2]=arr[$2]+($4-$3)} END{for (a in arr) print a"="arr[a]}''

I get this error.

bash: syntax error near unexpected token `('

Advertisement

Answer

Avoid using alias (they’re only allowed in interactive shells) and define a simple function instead to do

doSomething() {
    curl -XGET link | jq '.offsets[] | " (.topic) (.offset) (.logSize) "' |
       awk 'NR>1{arr[$2]=arr[$2]+($4-$3)} END{for (a in arr) print a"="arr[a]}'
} 

and defined in any of your startup configuration files(e.g. .bashrc for instance) and launch and new shell and do just

doSomething
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement