search
HomeOperation and MaintenanceLinux Operation and MaintenanceLinux operation and maintenance shell variables.md
Linux operation and maintenance shell variables.mdJun 24, 2020 am 09:30 AM
linux operation and maintenanceshell

We know that there are many variables in the shell, such as the PATH variable that we often use. Its function is to set the directory of the executable file, so that you do not need to use the absolute path when entering commands. In addition, the shell has many other variables. Today, let’s discuss shell variables with you.

Classification

We know that there is a distinction between local variables and global variables in php. The shell is very similar to php. There are environment variables, Ordinary variables. Environment variables are generally used to define the running environment of the shell, while ordinary variables are often used in writing shell scripts.

The difference between environment variables and ordinary variables is very similar to PHP variables: the difference is the scope of use. Shell environment variables can be used in the current shell and derived shells, while ordinary variables can only be used in The current shell is used. Environment variables usually use uppercase, and ordinary variables usually use lowercase.

Environment variables

To view all current environment variables, use the env (environment) command

# env
XDG_SESSION_ID=38135
HOSTNAME=iz8vb626ci0aehwsivxaydz
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=114.106.186.229 31955 22
QTDIR=/usr/lib64/qt-3.3
……

In addition You can use the set command to view it, but this command will list all environment variables and ordinary variables. Generally, environment variables are represented by uppercase letters.

Setting and canceling environment variables

The user-defined way to set environment variables is as follows

export 变量名=变量值

If you want to cancel environment variables , you can use the unset command to complete

unset 变量名

Let’s demonstrate below:

# export HOBBY=basketball
# env | grep HOBBY
HOBBY=basketball

# unset HOBBY
# env | grep HOBBY

Printing and setting of variables

There is an echo command under Linux. The usage method is the same as PHP. It is used to print a piece of text.

# echo hello,world
hello,world

# echo -e "$PWD\n$PATH"
/root
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Variable setting rules,

变量名=变量内容
  • Note "=" There cannot be spaces on both sides

  • The variable name can only It is letters and numbers, and the first character cannot be a number

  • If the variable content has spaces, you need to use quotation marks (single quotation marks, double quotation marks) to enlarge the variable content, such as name=" lebron james". Variables can be parsed within double quotes.

  • If there are special characters, you can use \ to escape them, such as enter, ', " and other special characters

  • If you want to use the command results As the variable content, you can use $(command) or command;

  • If you want to expand the variable content, you can use "{$variable}accumulated content"

array

Like PHP, shell variables also have array types. The definition syntax of an array is as follows:

变量名=(val1 val2 ... valn);
arr=(paul james durant)

The syntax for printing array elements is as follows

echo ${arr[元素下标]}
# echo ${arr[0]}
paul
# echo ${arr[1]}
james

Print all elements of the array

echo ${arr[@]}

Print the length of the array

echo ${#arr[@]}

Print the length of the array elements

echo ${#arr[数组下标]}

Print some elements of the array ${lnmp[@]:n1:n2} n1 represents the start, n2 represents the length

# 打印所有元素
# echo ${lnmp[@]}
linux nginx mysql php
# echo ${lnmp[@]:0}
linux nginx mysql php
 
# 打印从第二个元素开始的所有值
# echo ${lnmp[@]:1}
nginx mysql php
 
# 打印第一个值和第二个值
# echo ${lnmp[@]:0:2}
linux nginx
 
# 打印第二个值和第三个值
# echo ${lnmp[@]:1:2}
nginx mysql

Assignment, replacement and deletion of array

#追加元素
# lnmp[${#lnmp[@]}]=apache
# echo ${lnmp[@]}
linux nginx mysql php apache
 
# 修改元素
# lnmp[0]=l
# echo ${lnmp[@]}
linux nginx mysql php
 
# 删除元素
unset lnmp[0]
unset lnmp[1]
unset lnmp

The above is the detailed content of Linux operation and maintenance shell variables.md. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 Windows 11 上安装经典 Shell?如何在 Windows 11 上安装经典 Shell?Apr 21, 2023 pm 09:13 PM

<p>定制您的操作系统是让您的日常生活更加愉快的绝佳方式。您可以更改用户界面、应用自定义主题、添加小部件等等。因此,我们今天将向您展示如何在Windows11上安装ClassicShell。</p><p>该程序已经存在了很长时间,并允许您修改操作系统。志愿者现在已经开始运营该组织,该组织于2017年解散。新项目名为OpenShell,目前在Github上可供感兴趣的人使用。</p>&a

Explorer.exe 在系统启动时不启动 [修复]Explorer.exe 在系统启动时不启动 [修复]Jun 03, 2023 am 08:31 AM

如今,许多Windows用户开始遇到严重的Windows系统问题。问题是系统加载后Explorer.exe无法启动,用户无法打开文件或文件夹。虽然,Windows用户在某些情况下可以使用命令提示符手动打开Windows资源管理器,并且每次系统重新启动或系统启动后都必须这样做。这可能是有问题的,并且是由于下面提到的以下因素造成的。损坏的系统文件。启用快速启动设置。过时或有问题的显示驱动程序。对系统中的某些服务进行了更改。修改后的注册表文件。请记住以上所有因素,我们提出了一些肯定会对用户有所帮助

PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复May 10, 2023 am 11:02 AM

您在运行脚本时是否看到此错误消息“Add-AppxPackage:部署失败,HRESULT:0x80073D02,无法安装该包,因为它修改的资源当前正在使用中。PowerShell中出现错误0x80073D02…”?如错误消息所述,当用户在前一个进程运行时尝试重新注册一个或所有WindowsShellExperienceHost应用程序时,确实会发生这种情况。我们已经获得了一些简单的解决方案来快速解决这个问题。修复1–终止体验主机进程您必须在执行powershell命令之前结束

在 Windows 上运行 shell 脚本文件的不同方法在 Windows 上运行 shell 脚本文件的不同方法Apr 13, 2023 am 11:58 AM

适用于 Linux 的 Windows 子系统第一种选择是使用适用于 Linux 或 WSL 的 Windows 子系统,这是一个兼容层,用于在 Windows 系统上本地运行 Linux 二进制可执行文件。它适用于大多数场景,允许您在 Windows 11/10 中运行 shell 脚本。WSL 不会自动可用,因此您必须通过 Windows 设备的开发人员设置启用它。您可以通过转到设置 > 更新和安全 > 对于开发人员来完成。切换到开发人员模式并通过选择是确认提示。接下来,查找 W

超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!Apr 12, 2023 pm 01:52 PM

Python 脚本部分实例:企业微信告警、FTP 客户端、SSH 客户端、Saltstack 客户端、vCenter 客户端、获取域名 ssl 证书过期时间、发送今天的天气预报以及未来的天气趋势图;Shell 脚本部分实例:SVN 完整备份、Zabbix 监控用户密码过期、构建本地 YUM 以及上篇文章中有读者的需求(负载高时,查出占用比较高的进程脚本并存储或推送通知);篇幅有些长,还请大家耐心翻到文末,毕竟有彩蛋。Python 脚本部分企业微信告警此脚本通过企业微信应用,进行微信告警,可用于

以下是 Open Shell Windows 11 无法正常工作问题的修复以下是 Open Shell Windows 11 无法正常工作问题的修复Apr 14, 2023 pm 02:07 PM

无法在Windows 11上运行的 Open shell 并不是一个新问题,并且自从这个新操作系统问世以来一直困扰着用户。Open-Shell Windows 11 不工作问题的原因并不具体。它可能是由程序中的意外错误、病毒或恶意软件的存在或损坏的系统文件引起的。对于那些不知道的人,Open-Shell 是 2017 年停产的 Classic Shell 的替代品。您可以查看我们的教程,了解如何在 Windows 11 上安装 Classic Shell。如何替换 Windows 11 的开始菜

如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单Apr 18, 2023 pm 10:10 PM

OpenShell是一个免费的软件实用程序,可用于自定义Windows11开始菜单,使其类似于经典风格的菜单或Windows7样式的菜单。以前版本的Windows上的开始菜单为用户提供了一种浏览其系统内容的简单方法。基本上,OpenShell是ClassicShell的替代品,它提供了不同的用户界面元素,有助于从以前的Windows版本获取后一个版本的功能。一旦ClassicShell的开发在2017年停止,它就由GitHub志愿者以OpenShell的名义维护和开发。它与Win

反弹shell是什么意思反弹shell是什么意思May 11, 2023 pm 04:25 PM

*严正声明:本文仅限于技术讨论与分享,严禁用于非法途径。0x00前言反弹shell,就是控制端监听在某TCP/UDP端口,被控端发起请求到该端口,并将其命令行的输入输出转到控制端。通俗点说,反弹shell就是一种反向链接,与正向的ssh等不同,它是在对方电脑执行命令连接到我方的攻击模式,并且这种攻击模式必须搭配远程执行命令漏洞来使用。为什么要反弹shell?通常用于被控端因防火墙受限、权限不足、端口被占用等情形。假设我们攻击了一台机器,打开了该机器的一个端口,攻击者在自己的机器去连接目标机器,这

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software