0

I have a directory say "/dir". Inside this directory I have files with the name arg1_config.tcl, arg2_config.tcl, arg3_config.tcl. There might be more files going forward with the same extension. I am trying to dynamically generate aliases for arg1, arg2 and arg3 in this case and below is my code for the same.

foreach i (`ls <path>/dir/*.tcl`)
set app = `echo $i | sed -e 's/.*\///g' | sed 's/_config.tcl//g'`
echo "app is $app" 
alias $app 'run -app $app' # run is an internal script that takes arg1/2/3 as a switch
echo "alias $app 'run -app $app'"
end

When I source this file it prints

app is arg1
alias arg1 'run -app arg1'
app is arg2
alias arg2 'run -app arg2'
app is arg3
alias arg3 'run -app arg3'

However when I run which arg3 it says aliased to run -app $app and app value somehow is always the last value after exiting the foreach loop i.e arg3 in this case. I am not able to create aliases like the print messages above, i.e:

 alias arg1 'run -app arg1'
 alias arg2 'run -app arg2'
 alias arg3 'run -app arg3'
2
  • 1
    Using single quotes is preventing the variable from being expanded. Switch to double quotes. Commented Aug 23, 2017 at 0:25
  • Thanks Glenn! That was the problem. Switching to double quotes helped Commented Aug 23, 2017 at 6:51

1 Answer 1

2

I know it's a bash faq, but the ParsingLs guidelines apply here too. Don't parse the output of ls. Just don't. You have globs in csh too.

foreach i (`ls <path>/dir/*.tcl`)

should simply be

foreach i ( <path>/dir/*.tcl )

That said, the problem you're asking about is as Glenn suggested in comments. Single and double quotes behave in csh much the same way as they do in sh/bash: single quotes block variable expansion. Your alias is not actually setting an alias, it's setting something which when run will try to expand the variable at the time you run it. Try using double quotes and see if that gets you the behaviour you expect.

As an alternate strategy to shell aliases, consider linking or symlinking the script to multiple names on your path, then switching on $0 instead. It'll require less hacking on shells, which will be especially noticeable when someone decides to try out a different shell and forgets that these "commands" are really just shell aliases. :)

Sign up to request clarification or add additional context in comments.

3 Comments

That said, this deserves a read.
Thanks for the detailed description. Problem was infact with the single quotes. Switching to double quotes resolved it
@glennjackman, yes, that's why I added the csh tag to the question. The wordlist used by the foreach statement does pathname expansion (which csh calls "filename substitution"). And as I said in the answer, single and double quotes behave much the same way in csh as they do in bash, and the dangers of parsing ls are not exclusive to any shell, they have to do with the output of ls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.