Page 1 of 1

Environment variables with user commands. I.e. ]cd %temp%

Posted: Mon Jul 20, 2020 5:24 pm
by norbertjurkiewicz84
Should something like this work?

⎕cmd 'echo %cas%'
C:\github\cas-6.0
]cd
C:\Users\norbet\Desktop
]cd %cas%
* Command Execution Failed: Unable to change directory

Re: Environment variables with user commands. I.e. ]cd %tem

Posted: Tue Jul 21, 2020 1:16 pm
by Vince|Dyalog
Hi Norbert,

I have logged this as an RFE 18348.

Regards,

Vince

Re: Environment variables with user commands. I.e. ]cd %tem

Posted: Sun Jul 26, 2020 8:05 am
by Adam|Dyalog
In general, environment variable syntax is only recognised by shells and shell interfaces, not by applications, so this behaviour is consistent across all programs, by Dyalog and others.

The %name% syntax is specific to the Windows/DOS shells, while UX shells use $NAME, or ${NAME} if the name has unusual characters in it. SALT uses [name], but user commands in general don't support this. In any case, such a syntax might need a way to escape it if the literal characters are needed.

The formal way to achieve what you wrote is:
      ⎕SE.UCMD'CD ',⎕SE.Dyalog.Utils.Config'cas'

A couple of notes are in order:
  1. Changing directory while code is running can have unexpected consequences.
  2. It is considered bad practice to use user commands under program control.
Still, should you want to do this on a regular basis, you can roll your own user command pre-processor,
      '\[[^]]+]'⎕R{⎕SE.Dyalog.Utils.Config 1↓¯1↓⍵.Match}
which replaces text in square brackets with the corresponding environment variable.