Home > Back-end >  How to make certain command line arguments run by default for a program in linux?
How to make certain command line arguments run by default for a program in linux?

Time:05-22

So I'm using Ubuntu 20.04 in WSL2, and using the npm http-server (https://www.npmjs.com/package/http-server)

But, by default it enables caching, so I preferably want to disable caching by default. I know I can do it using

http-server -c-1

But is there any way to make that the default behaviour of http-server? So when I just run http-server, caching is disabled by default. I would like to not use aliasing to achieve this, instead using a bash script. I saw (in a course which had a codespace setup), that this was achieved by using a bash script, and whenever I ran which http-server, it would return something like /opt/name_conf/bin/http-server, which running http-server on my setup gives /usr/bin/http-server

How do I achieve the config that allows me to do this?

CodePudding user response:

You can do

#!/bin/sh
/path/to/http-server -c-1 "$@"

Name it http-server and make it executable and it must appear in a directory earlier in your PATH than the "real http-server.

I would use a shell function: add this to your ~/.bashrc

http-server() {
    command http-server -c-1 "$@"
}

In this case, you don't need to hardcode the full path to the real http-server: the command keyword ensures that the function does not call itself in an endless recursion.

CodePudding user response:

You can define an alias:

alias http-server="http-server -c-1"

This will be valid for the current shell session.

If you want to make this persistent for all your future shell sessions, add it to your ~/.bashrc, ~/.bash_profile, ~/.zshrc etc.

  • Related