How do I set the output of a function as a variable value in a bash script?

Arronical

I have the following function to count the number of files in a directory, within my bash script.

file_count() {
  no_of_files=$(find "$1" -maxdepth 1 -type f -printf '.' | wc -c)
}

I'd like to use it repeatedly on different directories and save the count into a variable for each directory. Currently to do this, I use

file_count $somedir
files_in_somedir="$no_of_files"

I'm aware that I'm setting the no_of_files variable outside of the function each time, and would like to make it local to the function, not settign an intermediate variable in the main script. This is just in case there's some mistake meaning that the variable doesn't change between calls of the function (mistyping the function name maybe), and the old value of no_of _files is used.

If my function were:

file_count() {
  local no_of_files=$(find "$1" -maxdepth 1 -type f -printf '.' | wc -c)
}

How would I easily set these directory count variables?

Byte Commander

Bash functions are not like functions on other programming languages, they are more like commands. This means they have no classical return value, but

  • an exit/return code. This is an integer number in the range 0-255, where 0 means "success" and every other value represents an error. If you try to specify a number outside this range, it will be taken modulo 256 (add or subtract 255 from your number until it fits the range 0-255).

    This code is automatically set to the return code of the last statement that got executed inside the function, unless you manually set it using the return command, like this:

    return 1
    
  • output streams. Each Bash function can write arbitrary strings to the output streams (STDOUT and STDERR), just like normal scripts. The output can either be directly from the commands you run in the function, or set manually by using echo.

    But instead of letting this output get displayed in the console, you can capture it when you run the function, e.g. using Bash's command substitution syntax, and store it in a variable in your main script:

    example_function() {
        # do something useful
        echo "return this message"
    }
    
    returned_value="$(example_function)"
    

So your code would have to look like this:

file_count() {
    find "$1" -maxdepth 1 -type f -printf '.' | wc -c
    # the line above already prints the desired value, so we don't need an echo
}

files_in_somedir="$(file_count "$somedir")"

See Return value in bash script (on Stack Overflow) for more info.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I set the output of a function as a variable value in a bash script?

From Java

How do I set a variable to the output of a command in Bash?

From Dev

How do I set the return value of a closure function to a variable?

From Dev

How do i store the output of AWK as a variable to use in a SED command in a BASH script?

From Dev

How do i grep a range of data value output by a program in Bash Script

From Dev

How do I assign this regex match from an OUTPUT value to a variable using bash?

From Dev

How do I write a bash script that will let users choose the value of the MIRROR environment variable?

From Dev

How do I set a bash variable that contains another variable?

From Dev

How do I make my bash script output data correctly?

From Dev

How do i store the output of a bash command in a variable?

From Dev

How do I set the output of exec to variable python?

From Dev

How do I set a variable to a command's output in csh?

From Dev

Set Bash script variable equal to function with parameters

From Dev

In a bash script, how do I check if "-e" is set?

From Dev

How do I set env variables in bash script for a packer image

From Dev

How do I write a Bash script that will ask users to define a variable?

From Dev

How do I have a variable in a bash script correspond to the url in wget

From Dev

Bash script - How do I redirect output from a bash script to a file?

From Dev

How do I set an environment variable in the shell to be result of python script?

From Dev

How do I redirect the output of a system() function to a variable?

From Dev

PHP OOP - How do I define a CONST with a Variable or Function Output?

From Dev

PHP OOP - How do I define a CONST with a Variable or Function Output?

From Dev

How do I assign a variable to a specific function output in python?

From Dev

How do I assign the Output of a command as a Value of a variable?

From Dev

How to redirect output of python script to bash variable?

From Dev

In bash how do I exit a script from a function that is piped by tee?

From Dev

How to set batch variable to output of another script

From Dev

How do I enter the variable value of my bash command into MySQL?

From Dev

how to gather first part of second line of output in a bash script and set to variable

Related Related

  1. 1

    How do I set the output of a function as a variable value in a bash script?

  2. 2

    How do I set a variable to the output of a command in Bash?

  3. 3

    How do I set the return value of a closure function to a variable?

  4. 4

    How do i store the output of AWK as a variable to use in a SED command in a BASH script?

  5. 5

    How do i grep a range of data value output by a program in Bash Script

  6. 6

    How do I assign this regex match from an OUTPUT value to a variable using bash?

  7. 7

    How do I write a bash script that will let users choose the value of the MIRROR environment variable?

  8. 8

    How do I set a bash variable that contains another variable?

  9. 9

    How do I make my bash script output data correctly?

  10. 10

    How do i store the output of a bash command in a variable?

  11. 11

    How do I set the output of exec to variable python?

  12. 12

    How do I set a variable to a command's output in csh?

  13. 13

    Set Bash script variable equal to function with parameters

  14. 14

    In a bash script, how do I check if "-e" is set?

  15. 15

    How do I set env variables in bash script for a packer image

  16. 16

    How do I write a Bash script that will ask users to define a variable?

  17. 17

    How do I have a variable in a bash script correspond to the url in wget

  18. 18

    Bash script - How do I redirect output from a bash script to a file?

  19. 19

    How do I set an environment variable in the shell to be result of python script?

  20. 20

    How do I redirect the output of a system() function to a variable?

  21. 21

    PHP OOP - How do I define a CONST with a Variable or Function Output?

  22. 22

    PHP OOP - How do I define a CONST with a Variable or Function Output?

  23. 23

    How do I assign a variable to a specific function output in python?

  24. 24

    How do I assign the Output of a command as a Value of a variable?

  25. 25

    How to redirect output of python script to bash variable?

  26. 26

    In bash how do I exit a script from a function that is piped by tee?

  27. 27

    How to set batch variable to output of another script

  28. 28

    How do I enter the variable value of my bash command into MySQL?

  29. 29

    how to gather first part of second line of output in a bash script and set to variable

HotTag

Archive