Reputation: 47
I try to pass an argument to the function install() but the output is "add" and not my argument. I don't know how to get just the REP when i call the function directory-install(), because for know i have all the phrase "add directory...."
function install () {
echo $1 #output not ok, show "add"
}
function directory-install () {
read -p "enter directory " REP
if cd $REP 2> /dev/null ; then
echo -e "add directory '$REP'\n"
else
mkdir REP
echo -e "creat directory \n"
fi
echo $REP
}
REP=$(directory-install)
echo $REP #output not ok too show "add directory..." but i just want the REP .
install $REP
Upvotes: 0
Views: 74
Reputation: 20909
Your install
function is working 100% correctly, the problem is that you're passing it garbage. Garbage in, garbage out.
The real culprit is your directory-install
function. When you execute REP=$(directory-install)
, the variable REP
now contains all of the text output from directory-install
, not just the final echo $REP
. That means the add directory
or creat directory
text.
If you want directory-install
to only return REP
, then you need to make sure that you have no other output in the function call. Alternately, you can redirect the non-return text to STDERR where it will be displayed, but not captured.
Example:
function badExample() {
echo "Hello World"
echo "ReturnText"
}
rtn=$(badExample)
# rtn now contains "Hello World\nReturnText"
function goodExample() {
echo "Hello World" 1>&2
echo "ReturnText"
}
rtn=$(goodExample)
# "Hello World" will appear on screen and rtn contains "ReturnText"
Upvotes: 1