Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

the questing is how to use two variables togheter..

ex. $1,$2,$3 are set

numb=2 

I need to use command:

head $$numb

that must be the same as:

head $2

I tried everything, I need to know what to put between $ and $numb so the output shows me the 10 first lines of $2

like head $"$numb" or head $'\$numb\'

but it doesn't work.

share|improve this question
    
Why don't you just do head $2 or numb=$2; head $numb? Please explain the rest of your script's structure, there will be other ways of doing what you need. Also, this is off topic here, you might want to flag for moderator attention and ask them to migrate to Unix & Linux or Stack Overflow. –  terdon Oct 13 '13 at 17:00
    
i set a text file (that contains locations of another files) to variables with command set 'less myfile' so every line of that file is $1,$2,$3... etc. then i set number of lines with command numb=$# .... gonna use also those commands: while [ $numb -gt 0 ] do head $"$numb" numb=$[$numb-1] so gonna make the script show the first then lines of every file. so the head $"$numb" not working as i wish, need help here –  Denis Oct 13 '13 at 17:22
    
OK, that is a really way too complex (and wrong, as you saw) way of doing this. See my answer for an easier method. –  terdon Oct 13 '13 at 17:41

2 Answers 2

up vote 1 down vote accepted

Without reading through all your comments, I address only your initial question.

In bashthe operator for variable indirection is !, see this simple example script foo:

#!/bin/bash
bar=2
echo "${!bar}"

As a side note: In (my favorite shell) zsh you can use the Parameter Expansion Flag P to get this result

#!/bin/zsh
bar=2
echo "${(P)bar}"

But you can also use a different approach, the third line may also read echo "${@[$bar]}". Here you can think of "$@" as an array of "$1" "$2" "$3" ..., hence "${@[2]}" is equivalent to "$2".

However, both foo scripts produce the same result:

$ ./foo one two three
two
share|improve this answer
    
wow that actually is what i was looking for for my first script thanx a lot ${!bar} did everything =) –  Denis Oct 13 '13 at 19:59

I think you are looking for something like this:

#!/bin/env/bash

files=$1

while IFS= read -r name;
do
  head "$name";
done < "$files"

Then, you run the script with the file containing the file names of interest (one per line) as an argument:

./foo.sh files.txt

The while loop saves each file name from files.txt as $name and then runs head on it.

share|improve this answer
    
Ok thanx that works, but i need the script to open the file that contains paths by itself and read the head of each file one by one, that why i was playing with those variables..so i want use a command after seeing the head of each file to remove it with the options yes and no...so its sort of script that will clean my directory so i was maybe thinking to use the case command for options –  Denis Oct 13 '13 at 18:06
    
@Denis you can do that with my example. Just change files=$1to files=/path/to/files.txt. Then, add the delete Y/N or whatever, after the head command inside the loop. The file name is $name and you can delete with rm $name. Next time, for this kind of thing, post on Unix & Linux or Stack Overflow. –  terdon Oct 13 '13 at 18:10
    
#!/bin/bash echo "Give directory to search for ASCII text files" read direct find $direct -type f -exec file {} + | grep ASCII > ztextz sed "s/ .*//" ztextz > ztextz1 sed "s/://g" ztextz1 > ztextz2 #lets try files=ztextz2 while IFS= read -r name; do head "$name"; echo "Do you want to delete this file?" read want case $want in YES) rm $name;; Yes) rm $name;; yes) rm $name;; Y) rm $name;; y) rm $name;; esac done < "$files" here is my script with yours. it searches for ASCII files and writes them in a file. Then it shows you head of first text file and ask if you want to delete and then next –  Denis Oct 13 '13 at 18:32
    
but still it doesnt work=( PS. sorry if i posted in a wrong place, its just my first post here... PSSS. and tell me how to write the script here as a script not as a text like i did –  Denis Oct 13 '13 at 18:34
    
@Denis please update your question to add new info, it is impossible to read and easy to miss in the comments. As for posting in the wrong place, don't worry, I know you did not do it on purpose, just letting you know for next time. However, since this question is likely to be closed here, flag it for mod attention and ask a mod to migrate it to Unix & Linux. I'm active there too so we can pick up where we left off. In the meantime, add what you have to your question and specify how it fails and what errors you get. –  terdon Oct 13 '13 at 18:38

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.