Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are "declare -f" and "declare -a" needed in bash scripts?

Sorry for the innocent question - I'm just trying to understand...

For example - I have:

$ cat test.sh
#!/bin/bash
declare -f testfunct

testfunct () {
echo "I'm function"
}

testfunct

declare -a testarr

testarr=([1]=arr1 [2]=arr2 [3]=arr3)

echo ${testarr[@]}

And when I run it I get:

$ ./test.sh
I'm function
arr1 arr2 arr3

So here is a question - why do I have to (if I have to ...) insert declare here? With it - or without it it works the same...

I can understand for example declare -i var or declare -r var. But for what is -f (declare function) and -a (declare array)?

like image 548
setevoy Avatar asked Nov 02 '13 12:11

setevoy


3 Answers

declare -f functionname is used to output the definition of the function functionname, if it exists, and absolutely not to declare that functionname is/will be a function. Look:

$ unset -f a # unsetting the function a, if it existed
$ declare -f a
$ # nothing output and look at the exit code:
$ echo $?
1
$ # that was an "error" because the function didn't exist
$ a() { echo 'Hello, world!'; }
$ declare -f a
a () 
{ 
    echo 'Hello, world!'
}
$ # ok? and look at the exit code:
$ echo $?
0
$ # cool :)

So in your case, declare -f testfunct will do nothing, except possibly if testfunct exists, it will output its definition on stdout.

like image 161
gniourf_gniourf Avatar answered Sep 27 '22 20:09

gniourf_gniourf


As far as I know, the -a option alone does not have any practical relevance, but I think it's a plus for readability when declaring arrays. It becomes more interesting when it is combined with other options to generate arrays of a special type.

For example:

# Declare an array of integers
declare -ai int_array

int_array=(1 2 3)

# Setting a string as array value fails
int_array[0]="I am a string"

# Convert array values to lower case (or upper case with -u)
declare -al lowercase_array

lowercase_array[0]="I AM A STRING"
lowercase_array[1]="ANOTHER STRING"

echo "${lowercase_array[0]}"
echo "${lowercase_array[1]}"

# Make a read only array
declare -ar readonly_array=(42 "A String")

# Setting a new value fails
readonly_array[0]=23
like image 31
eminor Avatar answered Sep 27 '22 20:09

eminor


declare -f allows you to list all defined functions (or sourced) and their contents.

Example of use:

[ ~]$ cat test.sh
#!/bin/bash

f(){
    echo "Hello world"
}

# print 0 if is defined (success)
# print 1 if isn't defined (failure)
isDefined(){
    declare -f "$1" >/dev/null && echo 0 || echo 1
}

isDefined f
isDefined g
[ ~]$ ./test.sh 
0
1
[ ~]$ declare -f
existFunction () 
{ 
    declare -f "$1" > /dev/null && echo 0 || echo 1
}
f () 
{ 
    echo "Hello world"
}

However as smartly said gniourf_gniourf below : it's better to use declare -F to test the existence of a function.

like image 21
Idriss Neumann Avatar answered Sep 27 '22 19:09

Idriss Neumann