Posts Tagged ‘var’

Bash gotcha: function variable scope

Posted: 11th October 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

Consider the following bash script: #!/usr/bin/env bash function myFunc {     myvar=123     echo “myFunc: setting myvar=$myvar” } myvar=1 echo “before myFunc: myvar=$myvar” myFunc echo “after myFunc: myvar=$myvar” The code here is fairly simple – we set a variable myvar, call a function and print the value of myvar to the terminal. However, even though we don’t […]

Using environment variables in C++

Posted: 26th February 2014 by Tim in C++
Tags: , , , , , ,

Sometimes you need to use environment variables from within your program. There are a few ways to get the environment into your program, but the most portable way is to use the getenv function. The function will return a pointer to the null-terminated string value, or NULL if the variable is not set. For example, […]

Reading environment variables in python

Posted: 13th November 2012 by Tim in Python
Tags: , , , , , ,

Shell envronment variables contain many useful paths and settings. At times you may want to use these variables from within a python program. This can be done via the os.path.expandvars(<var>) call, defined in os.path. For example, the following program: import os.path print os.path.expandvars(‘$SHELL’); Will print (on my machine): /bin/bash

Variable default values in Bash

Posted: 2nd October 2011 by Tim in Bash
Tags: , , , , , , ,

Sometimes you will be writing a script which, for example, can have some configuration changed from a command line argument. In traditional programming languages you would declare your variables with default values and then overwrite those values with the arguments which are passed in. In bash, however, you can do this all in one statement. […]