Windows终端变量与代理变量设置

Windows终端变量与代理变量设置

Windows常用终端:

  • 1、自带的cmd;
  • 2、自带的powershell;
  • 3、git bash(安装git之后带的);
  • 4、wsl:Windows Subsystem for Linux:windows上的Linux子系统,wsl2使用HyperV(Windows的虚拟化技术,Linux本质上相当于Windows中的一台虚拟机,只不过是自带的)。
  • 5、Windows Terminal,是微软开源的一款终端工具:https://github.com/microsoft/terminal,它只是一个终端工具,最终还是调用cmd、powershell、git bash这三者中的其中一种(就好像macOS上,无论你用自带的“终端”,还是用iTerm2,最终都是使用bash/zsh/fish等等这些shell).

个人建议使用git bash或Windows Terminal

cmd

# 设置变量a为hello
set a=hello

# 设置变量b为word
set b=world

# 把变量a字符串和变量b字符串连接起来(中间有个空格),最后赋值给变量c
set c=%a% %b%

# 分别打印变量a,b,c
echo %a%
echo %b%
echo %c%

# 删除变量a(直接把=号右侧留空即可)
set a=

在cmd中设置代理

set http_proxy=http://127.0.0.1:10809
set https_proxy=http://127.0.0.1:10809
set all_proxy=http://127.0.0.1:10808

# 取消设置代理
set http_proxy=
set https_proxy=
set all_proxy=

powershell

# 设置变量:以下两句等效,只不过第二句可以不用双引号
$str="hello"
set str "hello"

# 显示变量:以下三句等效(标准写法为第三种,但太麻烦了,我还是用第二种,跟Linux比较像)
$str
echo $str
Write-Output $str

# 连接两个变量:直接用+号(也可以用+=)
$str2 = "world"
$str3 = $str + $str2
$str3

# 删除变量:注意不能写$,推荐Remove-Variable写法
Remove-Variable str
del variable:str

在powershell中设置代理:无需设置,powershell会直接读取系统代理配置。

git bash

设置变量与linux一样

# 设置变量(等号两边不能有空格)
str="hello"

# 输出变量
echo $str

# 删除变量
unset str

# 连接两个变量
str2="world"

# 连接两个变量字符串,中间没有空格
str3=$str2$str3

# 连接两个变量字符串,中空有空格(一定要用双引号来设置一个空格,直接敲空格会报错)
str3=$str2" "$str3

# 但是直接设置的变量是局部变量,要导出(export)变量才能被curl等网络命令读取
export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809
export all_proxy=http://127.0.0.1:10808

# 取消设置变量的话用unset
unset http_proxy
unset https_proxy
unset all_proxy

WSL

WSL就是个Linux,所以一切与Linux相同,即与git bash相同,但问题在于,wsl2是用HyperV虚拟技术实现的,它与Windows本身网络是隔离的(wsl1不隔离),所以如果你需要在wsl2中使用Windows中的代理,那么你就需要走局域网ip(即把windows和wsl看成是两台不同的电脑)。

Windows Terminal

由于Windows Terminal本身只是个终端,它一定需要启动一个cmd、powershell、git bash其中一个,所以它启动的哪个,设置变量方式就与哪个相同。

cmd/powershell/git bash区别

git bash与Linux/Unix/macOS行为一致,变量设置方法与bash相同。

对于设置变量:

  • git bash设置变量直接写变量名=变量值,无需set命令;
  • cmd比git bash多一个set关键字;
  • powershell比git bash多一个$符号开头

对于打印变量:

  • git bash与powershell相同,都用echo打印,变量需要加$才能引用;
  • cmd也使用echo打印,但变量需要在变量名前后各加一个%才能引用;
  • 另外powershell也可以不用echo。
打赏
订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x

扫码在手机查看
iPhone请用自带相机扫
安卓用UC/QQ浏览器扫

Windows终端变量与代理变量设置