The Bash shell provides some variables that are prefixed with ‘~’ (named tilde) which is called Tilde Expansions.

They are synonyms for contents of other variables within your shell.

Tilde expansion is the process of converting these abbreviations to the directory names that they stand for. In this article, let us review the tilde expansion feature with the examples. Tilde expansion applies to the ‘~’ plus characters includes +, – and N (which is integer) up to whitespace or a slash.

The tilde expansion is used to expand to several specific pathnames:

Home directories

Current/previous working directory

Directories from directory stack.

Home Directories

Tilde expansion provides a way to expand the home directory of the current user or the home directory of the given user name.

Syntax

~ Expand to the variable $HOME or home directory of the current user

~USER Expand to the home directory of the given username

Example 1. Current Users Home

Tilde(~) as a separate word, expands to $HOME if it is defined, if $HOME is not defined, then it expands with the home directory of the current user.

Now the value of the HOME variable is /home/oracle so cd ~ changed the current directory to the value of $HOME.

## Logging into a oracle user, whose home directory is /home/oracle

# su oracle

[tmp]$ pwd

/tmp

[tmp]$ echo $HOME

/home/oracle

[tmp]$ cd ~

[~]$ pwd

/home/oracle

HOME is changed to /sbin, and cd ~ uses only $HOME not the home directory of the user. After unset the value of $HOME, cd ~ changed the directory to the value of the home directory set for the oracle user in /etc/passwd. For Tilde expansion, HOME overrides the real home directory.

[~]$ export HOME=/sbin

[oracle]$ cd ~

[~]$ pwd

/sbin

[~]$ unset HOME

[sbin]$ cd ~

[oracle]$ pwd

/home/oracle

Example 2. Home directory of the given user

The following script takes the backup of a log file that has the current date in the name. It also logs the starting time and end time into the file called backup.log in the home directory of the oracle user.

#! /bin/bash

echo "Initiating the backup at `date`" >> ~oracle/backup.log

da=`date +%F`

cp $da.log{,.bak}

echo "END BACKUP at `date`" >> ~oracle/backup.log

$ ls -l /home/oracle/

total 8

-rw-r--r-- 1 root root 99 Jun 4 14:23 backup.log

If the given user name does not exist then it doesn’t expand to something. In the following example, there is no user called ora. So, ~ora will not expand to /home/ora.

$ echo ~ora

~ora

Refer to our earlier article to understand how to perform brace expansion in bash. i.e how to use { } in bash.

Working Directories

Tilde with + and – are used for representing the working directories.

~+ expands to the value of the PWD variable which holds the current working directory.

~- expands to the value of OLDPWD variable, which holds the previous working directory. If OLDPWD is unset, ~- is not expanded.

Example 3. Expansion of old/current working directories

The following example will compare the file in the current directory and previous working directory.

$ cat comp.sh

#! /bin/bash

set -x

cd /var/opt/gg

if [ -f gg.c ]

then

echo "File1 exists"

fi

cd /var/opt1/gg

if [ -f gg.c ]

then

echo "File2 exists"

cmp ~+/gg.c ~-/gg.c

fi

$ ./comp.sh

+ cd /var/opt/gg

+ '[' -f gg.c ']'

+ echo 'File1 exists'

File1 exists

+ cd /var/opt1/gg

+ '[' -f gg.c ']'

+ echo 'File2 exists'

File2 exists

+ cmp /var/opt1/gg/gg.c /var/opt/gg/gg.c

cmp: EOF on /var/opt1/gg/gg.c

$

In the above execution:

~+/gg.c expands to /var/opt1/gg/gg.c

~-/gg.c expands to /var/opt/gg/gg.c

This article is part of the on-going Bash Tutorial series.

Expansion for Directories in Stack

Each bash process contains a stack object that can be used to keep track of directories a script did visit while it is processing data of directory contents.

It is a very simple mechanism to be able to reference directories or to change back to directories one did visit before. Tilde expansion also provides expansion to the directories in the directory stack.

~+N Expands the Nth directory in the directory stack (counting from the left of the list printed by dirs when invoked without options), starting with zero.

~-N Expands the Nth directory in the directory stack(counting from the right of the list printed by dirs when invoked without options), starting with zero.

Review our earlier article to understand how to use dirs, pushd and popd commands to manipulate directory stack.

Example 4. Displays Nth directory from left using ~+

In the following example, directory stack has 4 directories. ~+2 gives you the directory path available in the second position from left starting with zero.

$ dirs -v

0 /sbin

1 /var/opt/midas

2 /var/opt/GG/bin

3 /root

$ cd ~+2

$ pwd

/var/opt/GG/bin

But top of the stack (zero position) will always have the current directory. So after the above execution, following is shown in the directory stack.

$ dirs -v

0 /var/opt/GG/bin

1 /var/opt/midas

2 /var/opt/GG/bin

3 /root

Example 5. Displays Nth directory from right using ~-

Following is similar to the above example. But, will consider the directories from the bottom of the stack because of the ~-.

$ dirs -v

0 /var/opt/GG/bin

1 /var/opt/midas

2 /var/opt/GG/bin

3 /root

$ cd ~-2

$ pwd

/var/opt/midas

$ dirs -v

0 /var/opt/midas

1 /var/opt/midas

2 /var/opt/GG/bin

3 /root

linux中波浪线是根目录吗,linux 波浪线 ~ 使用方法相关推荐

  1. Linux下dislocate命令用法,在 Linux 中遨游手册页的海洋 | Linux 中国

    原标题:在 Linux 中遨游手册页的海洋 | Linux 中国 Linux 系统上的手册页可以做的不仅仅是提供特定命令的信息.它们可以帮助你发现你没有意识到的命令. https://linux.cn ...

  2. linux使用grep查找文件内容,Linux中使用grep命令搜索文件名及文件内容的方法

    这篇文章主要介绍了Linux中使用grep命令搜索文件名及文件内容的方法,同时文中还介绍了将匹配结果高亮显示的方法,相当实用,需要的朋友可以参考下 从文件中搜索并显示文件名 当从多个文件中搜索时,默认 ...

  3. linux中磁盘分区命令是什么,linux中创建磁盘分区的命令是什么

    linux中创建磁盘分区的命令是fdisk.具体方法是:1.进入root,查看所有磁盘的分区情况:2.执行命令[fdisk /dev/sdc],对sdc磁盘进行分区:3.依次输入n.p创建主分区即可. ...

  4. linux vi 移动光标,linux中vi命令的光标移动操作linux网页制作 -电脑资料

    分享一篇关于linux中vi命令的光标移动操作的文章,有需要的朋友可以参考一下, 全屏幕文本编辑器中, 光标的移动操作无疑是最经常使用的操作了.用户只有熟练地使用移动光标的这些命令,才能迅速准确地到达 ...

  5. linux中打开pdf文件_在Linux中减少PDF文件大小

    linux中打开pdf文件 In our Linux system, If we have a large PDF file, we may want to reduce it's size. We ...

  6. linux中通过date命令获取昨天或明天时间的方法

    linux中通过date命令获取昨天或明天时间的方法 date命令可以获取当前的时间,通过man,可以看到date有很多参数可以用,很容易做到格式化 date +"%F" 输出格式 ...

  7. 在linux中建立一个vim的目录,Linux学习笔记一(目录结构、Vim编辑器、用户管理)...

    1.Linux介绍 linux是一个开源.免费的操做系统,其稳定性.安全性.处理多并发已经获得 业界的承认,目前不少企业级的项目都会部署到Linux/unix系统上. Linux主要的发行版: Ubu ...

  8. linux中info功能是什么意思,Linux中的info指令

    Info 是什么?info是一种文档格式,也是阅读此格式文档的阅读器:我们常用它来查看Linux命令的info文档.它以主题的形式把几个命令组织在一起,以便于我们阅读:在主题内以node(节点)的形式 ...

  9. linux中文件复的概念,诠释 Linux 中“一切都是文件”概念和相应的文件类型

    原标题:诠释 Linux 中"一切都是文件"概念和相应的文件类型 原文出处: Aaron Kili 译文出处:runningwater 在 Unix 和它衍生的比如 Linux 系 ...

  10. linux中fstab文件_如何在Linux上写入fstab文件

    linux中fstab文件 zentilia/Shutterstock.comzentilia / Shutterstock.com Adding a new hard drive or solid- ...

最新文章

  1. 计算机交换机配置实验心得,实验六 三层交换机的配置实验报告
  2. node+ejs模板引擎的应用
  3. 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net
  4. 计算机控制的工频机是什么,UPS 如何分类,工频机和高频机区别是什么?
  5. cpu高对计算机有什么影响吗,CPU损坏对电脑造成哪些影响
  6. react-性能优化
  7. SQL Server【三】连接查询
  8. vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动
  9. PyTorch学习笔记——词向量简介
  10. 移动端车牌识别,小功能大作用
  11. java yaml_Java 使用snakeyaml解析yaml
  12. SPSS19.0实战之聚类分析(转载)
  13. 【申报指南】国家高新技术企业的认定标准、认定条件及奖励政策
  14. 【Java成王之路】EE进阶第十篇 MyBatis查询数据库
  15. 公众号网页授权php,微信公众号里的PHP网站进行网页授权
  16. 171-辽宁移动魔百盒CM211-1-YS-S905L3B-RTL8822C线刷包
  17. 回路、简单回路、简单路径
  18. nextjs 基于 isomorphic-unfetch 封装自己的请求库
  19. 【无极低码】低代码平台开发日记,低代码平台之sql编程
  20. 新冠肺炎的中西医结合康复方案

热门文章

  1. 在虚幻引擎 4 中处理内存泄漏问题
  2. 音频特效:Flanger 和 Chorus
  3. Karplus-Strong 算法简单介绍和实现
  4. 大数据架构师学习方向---加油。
  5. kafka相关知识点总结
  6. POJ 3621 Sightseeing Cows [最优比率环]
  7. java中删除特定后缀名文件
  8. (多重背包+记录路径)Charlie's Change (poj 1787)
  9. 实现ip数据包抓取并分析_一些网站https证书出现问题的情况分析
  10. python是动态语言_Python是动态语言:动态添加或删除属性、方法