Simple trick to echo bash variable name and value

#!/bin/bash echo_var () { # comments: # - won't work properly with numbers and bash special characters: @,#,$,! ... # Functionality of throwing error in this case can be added easily # by additional check. # - with array variables it works only as: "echo_var {a[1]}" # - doesn't check for emptyness of variable. It can be done, but in a quite tricky manner # For this purpose plain old `[ -z var_name ]` is better. case $# in 1) eval "echo $1 is \$$1" ;; *) echo 'Wrong # of args: run as: "echo_var var_name" without $ sign' && return 1 ;; esac } # Example foo_name="foo_value" echo_var foo_name # produces # "foo_name is foo_value"
Easy helpful function to print value of variable. Works like this:
`echo_var var_name`
produces:
'var_name is $var_name'
Helped me when debugging bash scripts a lot.
Can be updated (see comment in script), but proved to work for me in this manner.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.