Bash variables: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(Created page with "== Finding defined variables == Assuming you have done something like foo="bar" at your bash prompt it is not always clear how to echo ALL the defined variables back from your...")
 
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<metadesc>Examples of working with bash variables</metadesc>
== Finding defined variables ==
== Finding defined variables ==
Assuming you have done something like foo="bar" at your bash prompt it is not always clear how to echo ALL the defined variables back from your bash session.  The following is a good way to get that information when needed.  This can also be useful for debugging scripts that you are in the process of building out.
Assuming you have done something like foo="bar" at your bash prompt it is not always clear how to echo ALL the defined variables back from your bash session.  The following is a good way to get that information when needed.  This can also be useful for debugging scripts that you are in the process of building out.
Line 9: Line 10:
<pre>
<pre>
declare -xp
declare -xp
</pre>
An alternate command that will also give nice and parsable results.  This is quite nice, as the loop itself will pull the values that compgen -v gave as just var names.
<pre>
compgen -v | while read line; do echo $line=${!line};done 
</pre>
== Convert upper to lower case in various ways ==
Why bother having multiple ways to do the same thing?  Merlin software on routers does not have the same tr and sed versions, and are not GNU compiled.  However awk in that case does still work with its builtin tolower logic within itself.  Always a good idea to know more than one way to do things.
<pre>
echo FooBar | awk '{print tolower($0)}'
foobar
</pre>
<pre>
echo FooBar | tr [:upper:] [:lower:]
foobar
</pre>
<pre>
echo FooBar | sed 's/[A-Z]/\L&/g'
foobar
</pre>
== Bash builtins ==
From [https://www.reddit.com/r/bash/comments/12z9snv/5_inbuilt_bash_variables_that_every_developer/| reddit of all places]
<pre>
man bash | awk '/  Shell Variables/,/  Arrays/' | more
</pre>
== Bash history ==
Looks like it can reside in different dot files.  For Ubuntu it is in ~/.bashrc
<pre>
HISTSIZE=2000
HISTFILESIZE=4000
</pre>
</pre>


[[Category:Bash]]
[[Category:Bash]]

Latest revision as of 15:02, 25 April 2024

Finding defined variables

Assuming you have done something like foo="bar" at your bash prompt it is not always clear how to echo ALL the defined variables back from your bash session. The following is a good way to get that information when needed. This can also be useful for debugging scripts that you are in the process of building out.

declare -p

If you only want the environment variables, and do not want to use env for some reason

declare -xp

An alternate command that will also give nice and parsable results. This is quite nice, as the loop itself will pull the values that compgen -v gave as just var names.

compgen -v | while read line; do echo $line=${!line};done  

Convert upper to lower case in various ways

Why bother having multiple ways to do the same thing? Merlin software on routers does not have the same tr and sed versions, and are not GNU compiled. However awk in that case does still work with its builtin tolower logic within itself. Always a good idea to know more than one way to do things.

echo FooBar | awk '{print tolower($0)}'
foobar
echo FooBar | tr [:upper:] [:lower:]
foobar
echo FooBar | sed 's/[A-Z]/\L&/g'
foobar

Bash builtins

From reddit of all places

man bash | awk '/   Shell Variables/,/   Arrays/' | more

Bash history

Looks like it can reside in different dot files. For Ubuntu it is in ~/.bashrc

HISTSIZE=2000
HISTFILESIZE=4000