让我们看看在 cross-env 出现之前,人们是怎么在不同的平台上设置环境变量、运行命令。
Windows 命令提示符
在命令提示符(cmd)中执行多个命令
| Character | Syntax | Definition |
|---|---|---|
& [...] |
command1 & command2 |
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command. |
&& [...] |
command1 && command2 |
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully. |
| ` | [...]` | `command1 |
( ) [...] |
(command1 & command2) |
Use to group or nest multiple commands. |
; or , |
command1 parameter1;parameter2 |
Use to separate command parameters. |
翻译一下就是:
command1 & command2: 依次执行 command1、command2。command1 && command2: 执行 command1,当 command1 执行成功时,执行 command2。command1 || command2: 执行 command1,当 command1 执行失败时,执行 command2。
&& 和 || 实际上就是 与、或 逻辑运算。
在命令提示符(cmd)中设置环境变量
set variablename=value # 增加变量
set VariableName # 查看变量
set VariableName= # 删除变量Windows 命令不区分大小写,
SET同样有效。
在命令提示符(cmd)中的使用示例
set DEBUG=server:* && npm start
set NODE_ENV=production && node app.jsWindows PowerShell
在 PowerShell 中设置环境变量
$env:NODE_ENV="production"
$env:NODE_ENV=""Shell
在 Shell 中执行多个命令
;: 各个命令都会执行,但不保证每个命令都执行成功。各命令的执行给果,不会影响其它命令的执行。&&: 若前面的命令执行成功,才会去执行后面的命令。||: 只有前面的命令执行失败后才去执行下一条命令,直到执行成功一条命令为止。&: 在命令或者脚本后面增加&符号,可以使命令或脚本在后台执行。
在 Shell 中执行多行命令
Shell 支持转义,输入 \ 后再敲回车,可以在新行中继续输入命令。新行前一般会有新行提示符,如 >。
$ ls
$ l\↵
> sWindows 命令提示符(cmd)中命令行换行符为
^。
在 Shell 中设置环境变量
NODE_ENV=production只对本进程有效。export NODE_ENV=production对子进程同样有效。
Shell 使用示例
date whoami # 会报错
date; whoami# macOS/Linux/MSYS2
# 只对本行的命令有效
NODE_ENV=production node app.js
# 对当前 shell 有效
export NODE_ENV=production
node app.js
DEBUG=server:* npm start
DEBUG=server:*; npm start # 两者独立,环境变量不会对后面的命令产生影响
export DEBUG=server:*; npm start
export DEBUG=server:* && npm start其他使用示例
export LIB_THEME=light && npm run build && cp ./dist/index.css ./dist/index.light.css ; cp ./dist/index.css ./src/index.light.css ; export LIB_THEME=darkset LIB_THEME=light && npm run build && cp ./dist/index.css ./dist/index.light.css ; cp ./dist/index.css ./src/index.light.css ; set LIB_THEME=dark