drdot
drdot

Reputation: 3347

How to use set to change a shell variable?

I am using GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu).

$ set | grep SHELL
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor:verbose

Now I want to change the value of the SHELL variable to /bin/dash. So I tried

$ set SHELL=/bin/dash
set SHELL=/bin/dash
$ set | grep SHELL
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor:verbose
_=SHELL=/bin/dash

I also tried several other syntax. But the SHELL variable value just doesn't change. I have two questions:

  1. How to use set to change the SHELL variable
  2. What is _=SHELL=/bin/dash?

Upvotes: 2

Views: 4826

Answers (3)

RJHunter
RJHunter

Reputation: 2867

In Windows CMD shell, or the Unix csh shells, the set command sets variables.

However, in Bourne-style shells such as bash and dash, you can set variables without any particular command. Setting an environment variable can be as simple as:

MY_VARIABLE=some_value

The set command means something quite different indeed. It's mostly useful in shell scripts, rather than in interactive shells. You use it in a shell script to set the "positional parameters" (as in first argument, second argument, etc). You can also use it to set the shell options like tracing mode (-x), or fail-on-error (-E).

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

The set built-in command has a number of purposes, such as turning tracing on (set -x) or off (set +x). But it also sets the positional parameters — $1, "$@", etc. And that's what you've done with:

set SHELL=/bin/dash
echo "$1"

You'll get SHELL=/bin/dash as the output from the echo.

To set a variable, you set the variable — without any keywords:

SHELL=/bin/dash

The $_ is the value of the last argument of the previous command. For example:

$ cp oldfile.c newfile.c
$ vim $_
…now editing newfile.c
$

This can be useful, but you can also land yourself in problems because $_ changes when you didn't expect it to.

Note that if you were misguided enough to be using a C shell or C shell derivative, then you would use set variable=value to set variables.

Upvotes: 5

Baba Rocks
Baba Rocks

Reputation: 171

chsh -s {shell-name} {user-name}

use this if you want to change your default shell to dash

Upvotes: 0

Related Questions