Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Can I use a variable with brace expansion? Post 302544038 by DeCoTwc on Tuesday 2nd of August 2011 06:38:49 PM
Old 08-02-2011
Can I use a variable with brace expansion?

So, I was bored on the train today, and was thinking of ways to loop through elements of an array. I came up with the following simple script, but it doesn't work as brace expansion doesn't seem to work with variables. Is there something I'm missing, or does the shell just not work like this?

Code:
(18:33:24\[D@DeCoWork15)
[~]$ cat prac.sh
#!/bin/bash

x=(aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx  yy zz)
CAP=${#x[*]}
echo $CAP
for i in {1..$CAP};do echo ${x[${i}]};done

(18:35:19\[D@DeCoWork15)
[~]$ bash prac.sh
26
prac.sh: line 6: {1..26}: syntax error: operand expected (error token is "{1..26}")

So, clearly it is recognizing the variable, but it's not expanding the 1..26. Any thoughts?

Also, I'm aware that this will leave me with the issue that I'm expanding 1-26 when what I really want is 0-25...but I'll burn that bridge later.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example: script -s hello -t goodbye '*.d*' In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
Discussion started by: dangral
4 Replies

2. Shell Programming and Scripting

Basic variable expansion not working...

#!/usr/bin/bash if then echo "Not valid arguments entered. Just username should be entered." else USER_NAME=$1 FILE_NAME=$USER_NAME.info UNN=STUDIN\\\\$1 echo $UNN last STUDIN\\\\$1 last UNN If I type `last STUDIN\\eip060` it works but if I try to expand it with variable it is... (5 Replies)
Discussion started by: Zammy_bg
5 Replies

3. Shell Programming and Scripting

Brace expansion problem in Bash

I have a script that takes an option for server pools to run the script against. The option is given as a comma separated list (ie, -p 201,204,301). I'm using eval and brace expansion to get those pool numbers into an array. It works fine unless only 1 pool number is given. Here's the code: ... (5 Replies)
Discussion started by: mglenney
5 Replies

4. UNIX for Dummies Questions & Answers

Variable brace expansion

I'm in the habit of using the following type of loop structure: for num in `seq $1 $2` do command doneWhile `seq $1 $2` is not exactly a huge resource hog, I would like to learn a better way. It seems that brace expansion is a good way to go: for num in {3..10}The problem, though, is... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Shell Programming and Scripting

Find closing brace "{" of a given open brace "{"

There is a file as: ....... some text timing () { capacitance : 9.0; incap : 0.8; cell_fall () { values ("8.9","7.8"); } } ........ some more text ####### Is there a way to directly find closing brace "{" of timing () block "{" ? (2 Replies)
Discussion started by: nehashine
2 Replies

6. Shell Programming and Scripting

Bash variable delayed expansion?

i write a batch file , here is the content. dirname='date +%Y-%m-%d' mkdir dirname but it doen's work, it just create a folder named date and +%Y-%m-%d. i have tried run the command seperately in the bash prompt. after the first sentence executed , i use $dirname to watch the value of... (4 Replies)
Discussion started by: premotheus
4 Replies

7. Shell Programming and Scripting

Variable expansion in sed

The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be... (4 Replies)
Discussion started by: tiggyboo
4 Replies

8. Shell Programming and Scripting

delay variable expansion

Hi forum, in my bash script I've many lines executing commands with redirection to log files. ... xyz_cmd 2>&1 > $BASENAME.$LINENO The trailing part of these lines doesn't look nice and I like to put it into a variable. The (not working) idea is something like that ... (3 Replies)
Discussion started by: wolfi089
3 Replies

9. Shell Programming and Scripting

Protecting variable indicator ($) from expansion

Hello, I use a lot this command to edit a bunch of files at once find . -name filename" | xargs -ifoo sh -c 'echo foo ; sed "s/pattern1/pattern2/" foo > ./tmp ; mv -f ./tmp foo' I'm trying to put a function on my .bashrc file. function loopSed() { local filename=$1 local... (2 Replies)
Discussion started by: phollox
2 Replies

10. Shell Programming and Scripting

Bash variable expansion

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1 I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )echo $SOME_LINE... (3 Replies)
Discussion started by: jcdole
3 Replies
KPSEWHICH(1)						      General Commands Manual						      KPSEWHICH(1)

NAME
kpsewhich - standalone path lookup and and expansion for kpathsea SYNOPSIS
kpsewhich [options] [filenames] DESCRIPTION
This manual page is not meant to be exhaustive. The complete documentation for this version of TeX can be found in the info file or manual Kpathsea: A library for path searching. kpsewhich is used as a standalone front-end of the kpathsea library that can be used to examine variables and find files. When the -format option is not given, the search path used when looking for a file is inferred from the name given, by looking for a known extension. If no known extension is found, the search path for TeX source files is used. OPTIONS
kpsewhich accepts the following options: -debug num Set debugging flags. -D num Use a base resolution of num; the default, set by the installer, is typically 600. -dpi num As -D. -engine string Set $engine in the environment, which is used in some search paths. -expand-braces string Print variable and brace expansion of string. -expand-path string Print complete path expansion of string. -expand-var string Print variable expansion of string. -format name Use file type name. See the info manual for a list of valid names, or use the -help option to print the list. -help Print help message and exit. -interactive Ask for additional filenames to look up. -mktex fmt enable mktexfmt generation. (fmt=pk/mf/tex/tfm) -mode string Set device name for $MAKETEX_MODE to string; no default. -must-exist Search the disk as well as ls-R if necessary. -no-mktex fmt disable mktexfmt generation. (fmt=pk/mf/tex/tfm) -path string Search in the path string. -progname string Set program name to string. -show-path name Output search path for file type name. See the info manual for a list of valid names, or use the -help option to print the list. -var-value variable Print the expansion of variable. -version Print version information and exit. SEE ALSO
mktexlsr(1), mktexmf(1), mktexpk(1), mktextfm(1). Kpathsea 6.1.0 1 March 2011 KPSEWHICH(1)
All times are GMT -4. The time now is 06:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy