Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I tried to make a (very simple) function to put in my .bashrc file to make a local backup of a file, but when I call the function and ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    7

    [SOLVED] .bashrc function tab completion problem

    Hi,

    I tried to make a (very simple) function to put in my .bashrc file to make a local backup of a file, but when I call the function and try to use tab completion I get the following error:

    Code:
    cp: cannot stat `help': No such file or directory
    The function I added is as follows:

    Code:
    function bk
    {
        \cp -f --backup=numbered ${1} ${1}.bak
    }
    Interestingly, the tab completion still technically "works" properly, but this message inserts itself into the tab-completed file name.

    So for example, if there is a file called "doc.txt" that I want to make a backup copy of, and at the prompt I type "bk do[tab]", I immediately see "bk docp: cannot stat `help': No such file or directory\nc.txt" (the "\n" there is actually a new line, it doesn't print "\n"). And then if I press enter, it performs the backup.

    Also interestingly, if I define this function at the prompt rather than in my .bashrc, tab completion works fine.

    Thoughts?

    Thanks.

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    not sure why you really need to perform that as a function. have you tried using it as an alias?
    Code:
    alias bk='cp -f --backup=numbered ${1} ${1}.bak'
    linux user # 503963

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    7
    Hi Scathefire.

    Thanks for the idea. However, as far as I understand, aliases in bash do not support argument passing. Thus, because I want to be able to use the argument multiple times (source and part of destination name), I needed to be able to reference it (as ${1}). This is why I was using a function rather than an alias.

    Also, I recently was pointed in the correct direction as to source of my problem. I was defining the function in my .bashrc before I was sourcing my /etc/bash_completion file. When I made sure to put my function definition after /etc/bash_completoin was sourced, everything worked properly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...