File: setenv.fish

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (42 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# localization: tier1
function setenv
    # No arguments should cause the current env vars to be displayed.
    if not set -q argv[1]
        env
        return
    end

    # A single argument should set the named var to nothing.
    if not set -q argv[2]
        set -gx $argv[1] ''
        return
    end

    # `setenv` accepts only two arguments: the var name and the value. If there are more than two
    # args it is an error. The error message is verbatim from csh.
    if set -q argv[3]
        printf (_ '%s: Too many arguments\n') setenv >&2
        return 1
    end

    # We have exactly two arguments as required by the csh `setenv` command.
    set -l var $argv[1]
    set -l val $argv[2]

    # Validate the variable name.
    if not string match -qr '^\w+$' -- $var
        # This message is verbatim from csh. We don't really need to do this but if we don't fish
        # will display a different error message which might confuse someone expecting the csh
        # message.
        echo "setenv: Variable name must contain alphanumeric characters" >&2
        return 1
    end

    # We need to special case some vars to be compatible with fish. In particular how they are
    # treated as arrays split on colon characters. All other var values are treated literally.
    if contains -- $var PATH CDPATH MANPATH
        set -gx $var (string split -- ':' $val)
    else
        set -gx $var $val
    end
end