user_mda
user_mda

Reputation: 19388

Unix variables - The correct way

Is this the recommended way of writing shell script?

#!/bin/bash
VAR_VERSION_NUMBER = "x.y"
cd /path/to/VAR_VERSION_NUMBER

Upvotes: 0

Views: 52

Answers (1)

quarterdome
quarterdome

Reputation: 1581

No, this is correct way:

#!/bin/bash

VAR_VERSION_NUMBER="1.3"
cd /path/to/file/${VAR_VERSION_NUMBER}/more/path

The things missing from your example:

  1. Dollar sign ($) in front of variable name (VAR_VERSION_NUMBER).
  2. There should be no space between variable name and equal sign (=) in assignment.
  3. It is a good practice to surround variable name in curly braces, but it is not needed here.

Also note that once the script is over and shell exits, you will be back to the original directory you started in. The effect of cd command is limited to the shell you script is running in, not to the shell you are launching your script from.

Upvotes: 3

Related Questions