Skip to main content
Performance note
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

To temporarily override a variable while executing a command, use this syntax1:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell. (Forking a Bash subshell should be cheap, relative to forking and execing a command such as awk. Anyway, shell programs routinely use external commands, and performance is generally not a concern until it proves to be a problem.)

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.


1 From bash(1):

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

To temporarily override a variable while executing a command, use this syntax1:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell.

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.


1 From bash(1):

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

To temporarily override a variable while executing a command, use this syntax1:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell. (Forking a Bash subshell should be cheap, relative to forking and execing a command such as awk. Anyway, shell programs routinely use external commands, and performance is generally not a concern until it proves to be a problem.)

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.


1 From bash(1):

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

Added citation for the syntax to temporarily override a variable
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

To execute a command while temporarily overridingoverride a variable while executing a command, use this syntax1:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell.

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.


1 From bash(1):

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

To execute a command while temporarily overriding a variable, use this syntax:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell.

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.

To temporarily override a variable while executing a command, use this syntax1:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell.

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.


1 From bash(1):

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

To execute a command while temporarily overriding a variable, use this syntax:

VAR=value command

That relieves you of the duty to save and restore $IFS.

get_version_number() {
    # set local variable to executed arguments that is passed in
    local command="$($@)"
    # set up IFS to split the string on empty space
    IFS=" " read -a array <<< "$command" 
    # echo out array at a particular indices that should be passed in  
    echo ${array[@]}
}

I'm not happy about the readability of $($@), nor do I like the idea of storing the entire command output in a variable when you only want the first line. An alternate approach is to pipe the command output to a subshell.

get_version_number() {
    $@ | while IFS=" " read -a array ; do
        echo ${array[@]}
        break     # Exit after processing the first line
    done
}

By the way, git -v is an error. You want git --version.