Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Since it is allowed to do a=5 ; echo $a, I was wondering whether or not it is possible to declare lists and other data structures (such as tuples, dictionaries) in the terminal?

And if so, are they mutable or they must be unset first?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You can also use array; the simplest method of creating an array variable is to assign a value to one of its indices:

TEST[0]=zero
TEST[1]=one
TEST[2]=two

or, with:

TEST=(zero one two)

now, you access it as follows:

${TEST[index]}

ie

echo "First Index: ${TEST[0]}"

or you can access all the items in an array in one of the following ways:

${TEST[*]}
${TEST[@]}

For more help, read this usefull guide

share|improve this answer
    
Very useful, thank you ! –  user253956 May 5 at 18:27
    
@user253956 You're welcome. –  girardengo May 5 at 18:31

Is it possible. You can use lists and its mutable.

Try something like this:

arr=(1 2 3)  
echo ${list}
echo ${list[1]}
arr[1]=4
echo ${list}
echo ${list[1]}
echo ${list} | xargs -n 1

And, for a dictionary you will need to build a hash function.

For know how to do this:
http://stackoverflow.com/a/3467959/1585576

Its easy to iterate over a list with xargs and you also can add unit tests to your script: https://code.google.com/p/shunit2/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.