linux - returning values in a bash function -


i'm working growing bash script , within script have number of functions. 1 of these functions supposed return variables value, running issues syntax. below example of code.

showtags() {     local tag=0     read tag     echo "$tag" }  selected_tag=$(showtags) echo "$selected_tag" 

pulled code linux journal article, problem doesn't seem work, or perhaps , im missing something. whenever function called script hangs , not output anything, need ctrl+c drop cli.

the article in question below.

http://www.linuxjournal.com/content/return-values-bash-functions

so question proper way return value? there better or more dependable way of doing this? , if there please give me example can figure out without using global variables.

edit:

the behavior of getting me now. using following script.

showtags() {         echo "hi"      local tag=0      read tag      echo "$tag" }              selected_tag=$(showtags) echo "$selected_tag 

basically happens bash act if read command taking place before echo tag @ top of function. pass read though run top echo, , complete rest of script. not sure why happening. happening in main script.

change echo "hi" echo "hi" >/dev/tty.

the reason you're not seeing $(showtags) captures standard output of function, , gets assigned selected_tag. don't see of until echo variable.

by redirecting prompt /dev/tty, it's displayed on terminal, not sent function's stdout, doesn't captured command substitution.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -