All posts tagged Bash

Bash like history search functionality for Zsh

Bash uses readline to do its line editing and history search functionality, which works pretty well in the past for me. To enable this, just put these 2 lines of configurations in your ~/.inputrc (the config for your readline):

# Enabling History Search
"\e[A":history-search-backward
"\e[B":history-search-forward

After restart your terminal or shell, you can now use Up Arrow and Down Arrow to navigate your bash history, the beauty of this is it will match the thing you typed between the prompt and the cursor (if typed nothing, it will just use your last command of the history).

This solution works so well that it increases a lot of my productivity and efficiency in the past and now it becomes a must-have for my shell features. Once I switched to Zsh, I was looking for a similar solution since the first day of using Zsh.

Turns out oh-my-zsh includes a default history search functionality already (up-line-or-beginning-search, down-line-or-beginning-search), which works well for a lot of people, but unfortunately not for me.

The issue for this command is it only matches the command, not the entire thing before your cursor, for example, if I typed:

cd code

Then do a tab, it will only match the command starts from “cd”, not the entire “cd code”. You may still call this a history search functionality, but for me, it’s not working as I expected since I come from the bash/readline world.
Continue reading →

Zhou Lu liked this post

Terminal.app echo breaks after ctrl-c

基本的症状和 这里描述的问题 如出一辙,我也是 tail -f log/development.log 后出的问题,同道中人啊,提问者自己提供了两种解决方法(自问自答?):

$ stty echo

上面这个确实work,cool!

$ irb

货真价实的rubist,还想的出来这样的解决办法,不错不错,我也试了下,很遗憾在我这并不work…

底下的评论以及种种迹象表面这并不是mac自带的老版本bash的bug,而是一个bash-completion的bug,所以另外一个很make sense的solution就是更新bash-completion,我个人比较prefer这个solution,当然你也可以用macports安装最新版本的bash替换系统自带的bash,喜欢折腾的尽管上。

$ sudo port -v selfupdate
$ sudo port install bash-completion

verify一下,active的是1.3的bash-completion就算大功告成了。

$ port installed | grep bash-comp
  bash-completion @1.1_6
  bash-completion @1.2_0
  bash-completion @1.3_0 (active)

Bash variable unset or empty?

前几天开发遇到的诡异问题,最后总结下来就是标题所描述的case,怎么判断一个bash的variable到底是unset (undefined)还是set (defined)了只是正好是empty (“”)呢?

为了避免环境不同以及软件版本不同造成更大的confusion,这里先统一一下环境,以下命令或script都在如下环境下运行:

$ uname -a
Darwin mac.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

[Source of the confusion: ” -z “]
第一个solution是test的-z,也是confusion产生的根源。

test的manual这样解释-z:

NAME
     test, [ -- condition evaluation utility

SYNOPSIS
     test expression
     [ expression ]

DESCRIPTION
     -z string     True if the length of string is zero.

Continue reading →