0

I am trying to write a function in bash that takes a space delimited string and outputs the 1st word in that string. I tried

function myFunc { a=$1; b=($a); echo ${b[0]};}

but with

myStr="hello world"
myFunc myStr

I get no output.

if I replace a=$1 in the function definition above with a="hello world", then I get the expected result on applying

myFunc

result: hello

Also if instead of passing myStr as an argument I pass in "hello world" directly, it also works:

myFunc "hello world"

result: hello

I tried all kinds of indirections with the exclamation points, as well as the expr constructions but to no avail. Finally the following seems to work:

function el_index { alias a=$1; b=($a); echo ${b[2]};}

I would like to gather here all other possible ways to accomplish the same thing as above. In particular, is there a way to bypass the intermediate variable a completely, and do something like b=(${$a}) in the second statement within the function body above? By the way, the construction

b=($a)

splits the string stored in a into words using the default delimiter, which is a single white space.

Thanks!

3 Answers 3

3

You don't "get no output" -- you do get the first word of the string you passed:

function myFunc { a=$1; b=($a); echo ${b[0]}; }
myStr="hello world"
myFunc myStr
myStr

If you're passing a variable name, you have to indirectly expand the variable in your function

function myFunc { a=${!1}; b=($a); echo ${b[0]}; }
# ..................^^^^^
myFunc myStr
hello

Here's another technique: it takes the variable name as the first parameter, then overwrites the positional parameters with the words in the value of the variable. The first word is then the first positional parameter

firstword() { set -- ${!1}; echo $1; }
firstword myStr
hello
Sign up to request clarification or add additional context in comments.

4 Comments

Oh nice! I thought I had to do !$1 for indirection of function argument. That's very useful to know.
Second one is quite hacky, but definitely worth noting for future situations:)
I would argue "hacky" -- for shells without arrays (i.e. /bin/sh) it's a useful technique.
Indirection is not present in the POSIX specification, so I don't think you can rely on indirection to work in /bin/sh. zsh, for instance, uses a different syntax.
1

You can get 1st word this way:

myStr="hello world"
echo "${myStr%% *}"
hello

And your function will be:

function myFunc { echo "${1%% *}"; }

1 Comment

You could also say echo "${1%% *}"; (eliminating the use of variable).
0

This seems rather simple if I understand you correctly. I also think your method works.

    function myFunc1 { 
    a="${1}"; b=( $a ); echo ${b[0]};
    }

    function myFunc2 { 
    echo "${1}" | awk '{ print $1 }'
    }

    function myFunc3 { 
    echo "${1%% *}"
    }

    myFunc1 "Hello World"
    # Which prints out: Hello

    myFunc2 "Hello World"
    # Which prints out: Hello

    myFunc3 "Hello World"
    # Which prints out: Hello

Output:

    ./test.sh
    Hello
    Hello
    Hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.