0

I have created an alias in UNIX in which regex is creating problem.
My intention is to use alias as _enable -input, the parameter should be passed with "-".
Not getting where the problem is.

alias is

alias _enable="function _enable() { [[ $1 =~ ^- ]] || { echo \"Use hyphen (-) before nodes.\"; return 1; } ; [[ $# = 1 ]] && { NODE=`echo ${1##*-}`; } || { NODE=\"\"; } ; $HOME/bin/command --command=enable --node=$NODE; }; _enable $1;"

The actual output is:

SS-04:~ # _enable 1
-bash: conditional binary operator expected
-bash: syntax error near `^-'
1
  • Why do you require a hyphen in the argument if you just strip it off inside the function? Commented Aug 5, 2015 at 18:23

1 Answer 1

1

You don't really need an alias and all that quotes escaping.

Just this function is suffice:

_enable() {
   [[ $1 == -* ]] || { echo "Use hyphen (-) before nodes."; return 1; }
   [[ $# -eq 1 ]] && NODE="${1##*-}" || NODE=""
   $HOME/bin/command --command=enable --node="$NODE"
}

Then you can call:

_enable 1
Use hyphen (-) before nodes.
echo $?
1
Sign up to request clarification or add additional context in comments.

2 Comments

@anubhava...if I am adding the function _enable in an executable file and then calling it, its working fine. Even the one which I have posted, that was aslo working fine as a script. But when I have put it inside an alias, in that case only I am facing issue. The one which you have posted, after adding this method to alias, below output is coming... SS-04:~ # _enable -p Use hyphen (‐) before nodes. -bash: -p: command not found
You do not need to put it inside an alias at all. Just add this function definition in your. bashrc file and it will be available every where in your shell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.