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

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 → echo breaks after ctrl-c

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

$ stty echo


$ irb


底下的评论以及种种迹象表面这并不是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


$ 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 (“”)呢?


$ 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 “]


     test, [ -- condition evaluation utility

     test expression
     [ expression ]

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

Continue reading →