What Do the Characters in the Bash Environment Variable $- Mean

What do the characters in the bash environment variable $- mean?

From man bash:

-

Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

So these are the current options that control the behavior of the shell. In particular:

  • h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.
  • i: The current shell is interactive
  • m: Job control is enabled
  • B: Brace expansion is enabled
  • H: History substitution like !-1

Allowed characters in Linux environment variable names

From The Open Group:

These strings have the form
name=value; names shall not contain
the character '='.
For values to be
portable across systems conforming to
IEEE Std 1003.1-2001, the value shall
be composed of characters from the
portable character set (except NUL
and as indicated below
).

So names may contain any character except = and NUL, but:

Environment variable names used by the utilities in the Shell and
Utilities volume of IEEE Std
1003.1-2001 consist solely of uppercase letters, digits, and the '_'
(underscore)
from the characters
defined in Portable Character Set and
do not begin with a digit
. Other
characters may be permitted by an
implementation; applications shall
tolerate the presence of such names.

So while the names may be valid, your shell might not support anything besides letters, numbers, and underscores.

Use environment variable in word within command


  • There are no environment variables in your code, only shell variables.

    • Shell variables may also be environment variables (exported variables such as $PATH that are visible to all child processes, whether they're shells or not), but that's not the case here.
  • Shell variable names must be disambiguated from adjacent characters that could be part of a syntactically legal variable name by enclosing them in {...} - ${i}, in your case.

  • Unless you specifically want shell-variable references (possibly embedded in unquoted tokens) subjected to shell expansions, notably word-splitting and globbing, double-quote them - "s${i}a" in your case.

  • Use an arithmetic, C-style loop to create a memory-efficient loop with a variable number of iterations.

To put it all together:

$ n=5; for (( i = 1; i <= n; ++i )); do echo "s${i}a"; done
s1a
s2a
s3a
s4a
s5a

Special characters in prompt variables changed?

You're using zsh, not bash. Description of prompt sequences is
explained in zshmisc(1) (type man 1 zshmisc too see it). For
reference, it says:

   %n     $USERNAME.
%m The hostname up to the first `.'. An integer may follow the `%'
to specify how many components of the hostname are desired.
With a negative integer, trailing components of the hostname are
shown.
%~ As %d and %/, but if the current working directory starts with
$HOME, that part is replaced by a `~'. Furthermore, if it has a
named directory as its prefix, that part is replaced by a `~'
followed by the name of the directory, but only if the result is
shorter than the full path; see Dynamic and Static named direc‐
tories in zshexpn(1).

What does $- mean in Bash?

$- prints The current set of options in your current shell.

himBH means following options are enabled:

  • H - histexpand: when history expansion is enabled
  • m - monitor: when job control is enabled
  • h - hashall: Locate and remember (hash) commands as they are looked up for execution
  • B - braceexpand: when brace expansion is enabled
  • i - interactive: when current shell is interactive

Why do Windows environment variable names use a special escape character?

'\' is the character typically used to escape special characters on Unix systems (or the language C, or any language inspired by C). Which is handy, because it's more or less the only use of that character in those systems / languages.

Windows, on the other hand, comes from a CP/M heritage, which uses '\' as path delimiter. (Edit: Not quite correct, see comment below.) Having to escape the path delimiter on each occurence would be a bit awkward, and I guess (!!) that's why they chose a different escape character.

Filter out non-aplhanumeric characters within Bash brace expansion


$ ENV_VAR=some-stuff
$ echo ${ENV_VAR//[^a-zA-Z]}
somestuff

Parameter expansion (that's the proper terminology for what you call "brace expansion", which is actually something else entirely) accepts modifiers, such as making the first character (or all characters) lower- or upper-case, eliminating a prefix or a suffix, and replacing a pattern with some other string (or with the null string). See "Parameter expansion" in the section "Expansion" in the manual page.

gVim access environment variable with special characters

For Vim, environment variables can only contain alphabets, digits and underscore. From :h expand-env:

…  Any non-id character (not a letter, digit or '_') may
follow the environment variable name. That character and what follows is
appended to the value of the environment variable. Examples:
:set term=$TERM.new
:set path=/usr/$INCLUDE,$HOME/include,.

So, with an expression like set something=$PortableApps.comDocuments, Vim sees only $PortableApps as the environment variable - .comDocuments is a string to appended to it. With echo $PortableApps.comDocuments, . is the string concatenation operator, so Vim tries to take the value of
$PortableApps and the variable comDocuments and join them. With echo $PortableApps\.comDocuments, it sees the environment varible $PortableApps, followed by something it can't make sense of: \.comDocuments.

If you have access to Vim's Python or Perl interfaces, you can use them instead. For example, after starting Vim with:

env foo.bar=blah vim

Both of the following gave the output blah:

:perl VIM::Msg($ENV{"foo.bar"})
:python import os; print os.environ['foo.bar']

Within Python, for example, you could assign the environment variable to a Vim variable:

:python vim.vars['foobar'] = os.environ['foo.bar']

Then, you could use foobar like any other Vim variable:

:echo foobar
blah

Of course, being on Windows, you would have to install Python or Perl to use this.



Related Topics



Leave a reply



Submit